0

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

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 Commented Jan 1, 2019 at 22:06

1 Answer 1

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();
}
7
  • Hi Milo, sorry for my late reply, I was just in the middle of a divorce... As I am not very strong in code, I will try to explain how I see it. Let's say my URL structure is something like this: URL.com/date/1-january URL.com//date/2-january etc. Each date is an individual post. 1: How would the script look like? - Should I take into consideration how the URL structure is? 2: How and where do I implement the script? 3: How secure is the script to future WP updates? I would love to pay you to help me get this working.... I really appreciate your help. Commented Jan 9, 2019 at 15:20
  • HI Milo, did you see my question above? I would really appreciate your feedback :-) Commented Jan 27, 2019 at 11:32
  • The above code would go in whatever template file you want to display the day's post. URL structure is irrelevant, it uses publish date to query the current post. You should create a child theme so whatever template changes you make will survive a theme update.
    – Milo
    Commented Jan 27, 2019 at 18:18
  • Alright Milo, thanks for your feedback. So if I understand this right, I insert the code in the 'Default theme' template... does it matter where I insert the code? I use Avada Theme from ThemeFusion, but I guess that doesn't matter. So when I make posts, each named for that specific date, and publish the posts on that specific date, the script will (re)-publish the relevant post, matching the date, every year... is that correctly understood? Thanks a lot :-) Commented Feb 4, 2019 at 13:56
  • Alright Milo, I installed the Avada Child Theme which was pretty easy, but now the theme only has two files in it: style.css and functions.php Should I insert your code into one of these two files, as the child theme now is my primary theme, or should I find the 'main parent theme' default template, and insert in somewhere there...? Thanks a lot for your feedback :-) Commented Feb 9, 2019 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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