WordPress 🕰️ Automatically republish articles and post types

Share on twitter
Share on linkedin
Share on facebook
Share on whatsapp
Share on email
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 post of your site WordPress. There is also a plugin WordPress for those who don't like to get their hands in the code.

Republish your articles automatically without plugin on WordPress

The process is simple. The first part of the code allows to add a cron (here a wp-cron more precisely) which will execute a function at regular intervals. The second part of the code consists of a query 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 wp-cron execution:

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

It is also possible to create your own intervals by following the steps below.

/**
 * 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 with the identifier " 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 will then be able to use the " weekly " interval as one of the three default intervals of WordPress (hourly, daily and twicedaily).

Republish Old Posts : the plugin WordPress 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
Developer WordPress
Julien MA Jacob - Developer WordPress
🧙♂️ Passionate about the web and programming with always lots of projects in mind 👨💻 | About page
  Discussion of the article
Subscribe to our newsletter
Notify of
guest
2 Comments
most popular
latest oldest
Inline Feedbacks
View all comments
bjm

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

bjm

Thank you Julien MA Jacob for this code.
I answer to myself.

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

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

However, all the articles end up with the same date of publication and that makes a mess of the order of the articles.
In order to avoid this, a pose must be made 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 are in order, but there's 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 you have to replace the code

by

Out WordPress has a function of automatic code correction that it carries out to each republication of the articles automatically and it erases the code that it considers incorrect, so the videos are removed.
This problem must certainly affect people who use the code you provided to rewind the last article.

The solution would be to temporarily disable the formatting of the code by wordpress when making the loop but for now I'm drying out.
If you have a track or a solution I'm a taker.