WordPress gets articles in the specified time period

WordPress comment one hundred and eighty-one Reading mode

If you need to call WordPress articles within a certain period of time, you can do so through the following code.

 WordPress gets articles in the specified time period - Picture 1

WordPress gets articles in the specified time period

 <? php $cat = '2'; //  Classification ID //Get subcategories to exclude subcategorized articles $args = array( 'parent' => $cat ); $categories = get_categories( $args ); $excludecat = array(); foreach ( $categories as $category ) { $excludecat[] = $category->cat_ID; } $args = array( 'cat'=>$cat,//Classification ID 'posts_per_page'=>'10',//Display the number of articles 'ignore_sticky_posts'=>true,//Exclude top 'category__not_in'=>$excludecat,//Exclude sub category articles 'date_query' => array( array( //Start Date 'after'     =>  array( 'year'  => '2022', 'month' => '12', 'day'   => '1', ), //End Date 'before'    => array( 'year'  => '2023', 'month' => '12', 'day'   => '31', ), 'inclusive'=>true,//including the current day ), ), ); $query = new WP_Query( $args ); ?> <ul> <? php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();?> <li> <a href="<?php the_permalink(); ?>" rel="bookmark"><? php the_title(); ?></ a> </li> <? php endwhile;?> <? php wp_reset_postdata(); ?> <? php else : ?> <li> No article </li> <? php endif;?> </ul>

Comments are added in the code, which can be deleted according to the actual situation. For example, if you do not want to exclude sub category articles, you can delete them:

 'category__not_in' => $excludecat,

The above code is only a basic writing method. For more parameters, please read: Official website document wp_query

WordPress获取指定时间段内的文章-图片2
Get a list of WordPress articles published this week.<? php $args = array( 'posts_per_page'...
four hundred and forty four
WordPress获取指定时间段内的文章-图片2
Get the list of articles published by WordPress that day.<? php $today = getdate(); $ args = arr...
two hundred and eight

Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!

 weinxin
My Wechat
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 anonymous

Comment

Anonymous netizens
 :?:  :razz:  :sad:  :evil:  :!:  :smile:  :oops:  :grin:  :eek:  :shock:  :???:  :cool:  :lol:  :mad:  :twisted:  :roll:  :wink:  :idea:  :arrow:  :neutral:  :cry:  :mrgreen:

Drag the slider to complete validation