In my previous post I mentioned a site that has a tag for a date. That is, it uses 25-march
(or march-25
) to tag it’s posts so someone could conceivably find all the posts made on March 25th in every single year.
WordPress makes it easy to list all the posts on a specific date. Just visit example.com/2018/04/19/
and you’ll see all the posts made on April 19th of this year. Remove the 19 and you get everything from April and so on and so forth.
But you can’t, out of the box, list everything on April 19th from every single year the site’s been up.
WP_Query has Dates
As of 3.7, WordPress has a date_query
aspect to WP_Query
which lets you do this:
$all_posts_on_this_date = new WP_Query( array( 'posts_per_page' => -1, // this will show all the posts 'date_query' => array( array( 'month' => date( 'n', current_time( 'timestamp' ) ), 'day' => date( 'j', current_time( 'timestamp' ) ) ), ), ) );
That generates an array of every single post that has the same month and day of today, but does not check the year. If you don’t want all the posts, just the last ten, use 'posts_per_page' => 10,
instead.
Once you have your list of posts, you can use a normal loop to display the content:
if ( $all_posts_on_this_date->have_posts() ){ while( $all_posts_on_this_date->have_posts() ) { $all_posts_on_this_date->the_post(); the_title(); } }