loop - Repost post on specific date every year

admin2025-06-04  1

I am creating a website about 'This Day in History' where I show daily events etc. all based on a particular date. Each post has to be re-posted every year, and a specific date so that everyday users see information relevant to today's date.

So if I have 365 posts, one for each day, each post must be the published post for that specific date. In other words, posts kind of rotate every day throughout the year. So every day a new post is published, the very post that matches that date... I hope I make my self clear :-)

Is there a way I can control how the posts are (re)-scheduled, so that they continue to get posted each year on that specific date?

Thanks

/Anders

I am creating a website about 'This Day in History' where I show daily events etc. all based on a particular date. Each post has to be re-posted every year, and a specific date so that everyday users see information relevant to today's date.

So if I have 365 posts, one for each day, each post must be the published post for that specific date. In other words, posts kind of rotate every day throughout the year. So every day a new post is published, the very post that matches that date... I hope I make my self clear :-)

Is there a way I can control how the posts are (re)-scheduled, so that they continue to get posted each year on that specific date?

Thanks

/Anders

Share Improve this question asked Dec 30, 2018 at 23:10 Anders OlsenAnders Olsen 1 2
  • A couple of other options without re-publishing- you can get the current date and query for a post on the same day in the year it was originally published. or you could use a taxonomy or post meta to store a date, and similarly query for the current day's post. – Milo Commented Dec 31, 2018 at 1:51
  • HI Milo, I'm sorry, but I don't fully understand what you mean... could you perhaps give me an example so it would be easier for me to implement? Thanks a lot /Anders – Anders Olsen Commented Jan 1, 2019 at 22:06
Add a comment  | 

1 Answer 1

Reset to default 1

Rather than re-post the posts, we can create a custom query for the current day's post regardless of year. We use current_time to get the current day and month according to the site's timezone settings, then we create a new query containing date parameters for month and day. We don't specify a year, so it'll return anything posted on this day from any year.

$day = current_time( 'j' );
$month = current_time( 'n' );

$args = array(
    'date_query' => array(
        array(
            'month' => $month,
            'day'   => $day,
        ),
    ),
);
$today = new WP_Query( $args );

if ( $today->have_posts() ) {
    while ( $today->have_posts() ) {
        $today->the_post();
        // output the post
        the_title();
    }
    wp_reset_postdata();
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749052462a315949.html

最新回复(0)