This is for get all posts of provided bunch years
$years = array(
'2018',
'2017',
'2016',
'2015',
);
foreach ( $years as $year ) {
$loop = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => -1,
'year' => $year
) );
//Other stuff.
}
This is for get all posts of provided single years
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'year' => $today["year"], //Or you can pass static date 2023 like
'order' => 'ASC',
'posts_per_page' => 3,
);
$posts = new WP_Query( $args );
This is for get all posts of provided specific month of year
$args = array(
'date_query' => array(
array(
'year' => 2023,
'month' => 02
),
),
);
$posts = new WP_Query( $args );
the current year is:'year' => $today["year"]
One year previous is:'year' => $today["year"]-1
Two years previous is:'year' => $today["year"]-2
etc.
Example
Below code add into custom template and confirm that get all posts of provided date
$today = getdate();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'year' => $today["year"], //Or you can pass static date 2023 like
'order' => 'ASC',
'posts_per_page' => 3,
'paged' => $paged
);
$posts = new WP_Query( $args );
if ( $posts-> have_posts() ) :
while ($posts->have_posts()) : $posts->the_post();
echo get_the_post_thumbnail();
echo the_category();
echo get_permalink();
the_title();
echo wp_trim_words(get_the_content(), 100);
echo get_the_author_meta('display_name', $author_id);
echo get_the_date();
echo get_the_time();
echo get_comments_number_text();
endwhile;
pagination_bar( $posts);
endif;
wp_reset_postdata();