WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] PHP call to repeat posts annually? (3 posts)

  1. Jonathan Landrum
    Member
    Posted 1 year ago #

    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?

  2. dc5ala
    Member
    Posted 1 year ago #

    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.

  3. Jonathan Landrum
    Member
    Posted 1 year ago #

    Thank you! I think that will work great.

Topic Closed

This topic has been closed to new replies.

About this Topic