1

I'm trying to figure out how to do this. I have many posts. I want it so that when someone pulls up the site, the front page shows all the posts that were done on the same month and day -- but disregards the year. So, if I were to pull up the website on Jan. 30, I would see all posts done on Jan. 30th of ANY year... a historical listing. I was also thinking of doing it by category, if the category was named Jan-30, is there a way I can get the system to take today's date and compare it to the categories, and show the one that matches (no year, of course)?

Can anyone help?

1
  • 1
    Okay, you guys are BRILLIANT. I actually tried all three methods (after Milo corrected my placement, LOL) and each one worked PERFECTLY. Thank you, Darren, Chris and Milo -- you saved my sanity! Jan 13 '15 at 5:18
4

You can achieve this with the date_query parameters added in version 3.7.

To modify the main query on your posts page before it is run and apply the date_query parameters, we use the pre_get_posts action:

function historical_posts_list( $query ){
    if( $query->is_home() && $query->is_main_query() ){
        $date_query = array(
            array(
                'month' => date( 'n', current_time( 'timestamp' ) ),
                'day' => date( 'j', current_time( 'timestamp' ) )
            )
        );
        $query->set( 'date_query', $date_query );
    }
}
add_action( 'pre_get_posts', 'historical_posts_list' );

If the page you want this on isn't your posts page, you can add a custom query in your template with the same parameters, and run a separate loop:

$posts_from_today = new WP_Query( array(
    'date_query' => array(
        array(
            'month' => date( 'n', current_time( 'timestamp' ) ),
            'day' => date( 'j', current_time( 'timestamp' ) )
        ),
    ),
    'posts_per_page' => -1,
) );

if( $posts_from_today->have_posts() ){
    while( $posts_from_today->have_posts() ){
        $posts_from_today->the_post();
        the_title();
    }
}
3
  • Hey Milo, I'm trying this out and I put it on the index.php since that's where my loop is (stop me if I'm putting it in the wrong place!). I wrapped it in php tags and put it before the loop starts. Now, I'm not getting any error but on the other hand, it's showing me all the recent posts and not today's, so I've clearly put it in the wrong location... help? Jan 13 '15 at 4:31
  • the first example would go in your theme's functions.php file.
    – Milo
    Jan 13 '15 at 4:40
  • See, that's why I needed you guys. <lol> Jan 13 '15 at 4:42
2
$q  = new \WP_Query([
  'monthnum' => date('n'), // beware the date function!
  'day'      => date('j'),
]);

while ($q->have_posts()) {
  $q->the_post();
  // ...
}
wp_reset_query();

See the documentation on date parameters.

Since you want to modify the home page query specifically, you might hook into pre_get_posts and modify the main query on the home page only.

add_action('pre_get_posts', 'wpse174669_pre_get_posts');
function wpse174669_pre_get_posts($q)
{
    if (is_home() && $q->is_main_query()) {
        $q->set('ignore_sticky_posts', true);
        $q->set('monthnum', date('n'));
        $q->set('day', date('j'));
    }
}
1

If you go the Category route, you could do something like this:

First, get a slug of today's date:

$date_slug = current_time('M-d');

Then, create a taxonomy query argument array:

$tax_query =  array(
    array(
        'taxonomy' => 'category',
        'field'    => 'slug',
        'terms'    => $date_slug,
    )
);

Then, build a query using the WP_Query class:

$r = new WP_Query(
    array(
        'tax_query' => $tax_query,
    )
);

Then, build your loop:

<?php if ($r->have_posts()) : ?>

    <?php while ( $r->have_posts() ) : $r->the_post(); ?>

    <?php endwhile; ?>

<?php endif; ?>

<?php wp_reset_postdata(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.