WordPress 🕰️ Automatically republish articles and post types

Share on email
Share on twitter
Share on linkedin
Share on facebook
Article How to automatically republish WordPress posts without a plugin by @wpr0ck
Article How to automatically republish WordPress posts without a plugin by @wpr0ck
Article How to automatically republish WordPress posts without a plugin by @wpr0ck
Article How to automatically republish WordPress posts without a plugin by @wpr0ck
Share on twitter
Share on linkedin
Share on facebook
Share on whatsapp
Share on email

A simple code to add to your functions.php file to regularly and automatically republish your articles or any other standard post on your WordPress site. There is also a pluginWordPress for those who don't like to get their hands on the code.

Republish your articles automatically without plugin on WordPress

The process is simple. The first part of the code allows you to add a cron (here a wp-cron more precisely) that will execute a function at regular intervals. The second part of the code, on the other hand, consists of a request that calls the oldest post and assigns it the current date.

**
 * Link posts_republish() with a wp_cron and execute it daily
 */
if ( ! wp_next_scheduled( 'posts_republish_hook' ) ) {
    wp_schedule_event( time(), 'daily', 'posts_republish_hook' );
}
add_action( 'posts_republish_hook', 'posts_republish' );

/**
 * Change the last posts date to today's date
 */
function posts_republish() {
    $args = array(
        'post_type'      => 'post',
        'posts_per_page' => 1,
        'order'          => 'ASC',
    );
    $my_query = new WP_Query( $args );

    while ( $my_query->have_posts() ) {
        $my_query->the_post();
        $update_post = array(
            'ID'        => get_the_id(),
            'post_date' => date( 'Y/m/d H:i:s' ),
            'post_date_gmt' => date( 'Y/m/d H:i:s' )
        );
        wp_update_post( $update_post );
    }
    wp_reset_postdata();
}

By default, WordPress offers 3 time intervals for running wp-cron:

  • hourly: every hour
  • daily: every day, it is the one used here
  • twicedaily: every other day

It is also possible to create your own intervals by doing the following

/**
 * Custom Cron Recurrences
 */
function custom_cron_job_recurrence( $schedules ) {
  $schedules['weekly'] = array(
    'display' => 'Once weekly',
    'interval' => 604800,
  );
  return $schedules;
}
add_filter( 'cron_schedules', 'custom_cron_job_recurrence' );

Here, we create a new interval whose identifier will be "weekly", the name displayed will be "Once weekly" and the time between each execution will be 604800 seconds, which is equivalent to 7 days. Once this code is added, you can then use the "weekly" interval as one of the three default WordPress intervals (hourly, daily and twicedaily).

Republish Old Posts: the WordPress plugin that republishes your articles

The Republish Old Posts plugin offers many options such as keeping the original date of the article or selecting the categories of the articles to be republished. However, to my regret, it only works on posts and does not allow you to republish your own standard posts.

Julien MA Jacob
WordPress Developer
Julien MA JACOB - WordPress Developer
Passionate about the web and programming with always many projects in mind
  Discussion of the article
Subscribe to the newsletter
Notify to
guest
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
bjm

And if we want to republish all the articles every 15 days, for example, without touching the hour, would that be possible?

bjm

Thank Julien MA JACOByou for this code.
I answer to myself.

My website is a showcase site so the publication date of my articles has no impotence, however the order in which they appear is important so I tried to modify your script to refresh the publication dates of all articles automatically every 14 days in order to appear in the search results the people who set Google's search parameters on "less than a month".

To do this, you must retrieve the number of articles published.
Then do a loop that will go back up the last article and do this as many times as there are articles published.

However, all articles are published on the same date and this makes a mess of the articles.
To avoid this, it is necessary to install between each loop.

We get the following code.

publish);
		while ($nombre_article >= 1)
		{
    	$nombre_article--;
		sleep(2);
        $args = array(
            'post_type'      => 'post',
            'posts_per_page' => 1,
            'order'          => 'ASC',
        );
        $my_query = new WP_Query( $args );
        while ( $my_query->have_posts() ) {
            $my_query->the_post();
            $update_post = array(
                'ID'        => get_the_id(),
                'post_date' => date( 'Y/m/d H:i:s' ),
                'post_date_gmt' => date( 'Y/m/d H:i:s' )
            );
            wp_update_post( $update_post );
        }
        wp_reset_postdata();          
    }
    }
//*** Fin de Republier vos anciens articles automatiquement  */

The articles keep their order, but there is a "but"

For the management of the RGPD I use the code "Include tarteaucitron.js"
https://opt-out.ferank.eu/fr/ and for YouTube videos I use the code

through

Hors WordPress has an automatic code correction function that it performs each time articles are republished automatically and it deletes the code it considers incorrect, so videos are removed.
This problem must certainly affect people who use the code you provided to trace back to the last item.

The solution would be to temporarily deactivate the formatting of the code by wordpress when the loop is created, but for the moment I'm drying out.
If you have a lead or solution, I'll take it.