• Resolved Jonathan Landrum

    (@jonlandrum)


    One of the sites I run has multiple annually repeating posts. I originally chose WordPress for the task because of how simple it would be to write those posts and later go back and change the year they publish to make them recycle in January. But is there a PHP call I can fabricate to return any post published on this date, regardless of year?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Not exactly sure what you have in mind. Write a plugin that automatically re-publish those posts? In a WP_Query there are query vars for day, month etc. which could be used in a hook like ‘pre_get_posts‘.

    Here’s an example if you want to query all posts that have the same day and month as today:

    <?php
    
    $args = array(
    	'post_type' => 'post',
    	'post_status' => 'publish',
    	'day' => date('j'), // Current day
    	'monthnum' => date('n'), // Current month
    	'posts_per_page' => 25, // Number of posts to show
    );
    
    $myQuery = new WP_Query( $args );
    if ( $myQuery->have_posts() ) {
    	while( $myQuery->have_posts() ) {
    		$myQuery->the_post();
    		echo get_the_date() . '<br />'; // As example just output the date
    	}
    }
    
    wp_reset_query();
    wp_reset_postdata();
    
    ?>

    If you want this to be automated, you could add a post meta information and provide a checkbox in a meta-box that marks this post to be repeated every year.

    Thread Starter Jonathan Landrum

    (@jonlandrum)

    Thank you! I think that will work great.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘PHP call to repeat posts annually?’ is closed to new replies.