$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>loop - Repost post on specific date every year|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

loop - Repost post on specific date every year

matteradmin11PV0评论

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();
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far