4

I'm using a free SEO plugin for WordPress called Rank Math - https://wordpress.org/plugins/seo-by-rank-math/

Overall, I like it a lot. But it has some problems for users with existing WP sites who are importing data from another plugin like Yoast.

Rank Math is relying on individual posts and pages to be edited (not using WP's bulk quick edit feature, but opening each post and re-saving them one-by-one).

This happens with author sitemaps for example. An author's page is only added to the sitemap if I manually open and re-save one of their posts and then re-save the sitemap settings. It doesn't find and add existing authors otherwise.

Is there any way to simulate clicking the "update" button for all posts and pages without changing any actual content in a circumstance like this?

Thanks.

2 Answers 2

7

Add to your functions.php file, but dont forget to take it off after use it !!!!!

function update_all_posts() {
    $args = array(
        'post_type' => 'post',
        'numberposts' => -1
    );
    $all_posts = get_posts($args);
    foreach ($all_posts as $single_post){
        $single_post->post_title = $single_post->post_title.'';
        wp_update_post( $single_post );
    }
}
add_action( 'wp_loaded', 'update_all_posts' );

PS : BE SURE TO MAKE A DATABASE BACKUP BEFORE EXECUTE !!!

12
  • Thanks. But as I mentioned in my question, the built-in bulk quick edit feature in WordPress does not work for this. It doesn't trigger any of the necessary actions from the plugin, like the rank calculation. It has to be triggered by the update button on every individual post and page.
    – JHM
    Commented Mar 5, 2019 at 16:02
  • sorry, didn't read enough ;) i've updated my answear with a function to load ! ( load one time, and then take of ;-) )
    – Gregory
    Commented Mar 5, 2019 at 16:10
  • Well, partial success! It did pull most of the authors into the author sitemap. For some reason it missed 5, but I'll manually catch those. It did not, unfortunately, trigger the score calculation for each post.
    – JHM
    Commented Mar 5, 2019 at 16:17
  • I think the missing 5 had to do with it being 1500 posts. It was loading for a while then crashed. I'll have to run something again for the score calculations, so for future reference, is there a way something like this could be run in batches so it doesn't cause that crash?
    – JHM
    Commented Mar 5, 2019 at 16:25
  • for this, you should have a look around of php_max_execution_time ! (or run it localy ).
    – Gregory
    Commented Mar 5, 2019 at 16:29
3

If you have a smaller server (such as Shared Hosting), you can use this WP-CLI command to update all the posts one at a time. That way, it handles memory without causing a "503 server error" and the like.

Create a new file in your theme such as /wp-content/themes/mytheme/commands.php:

// add this to your /wp-content/themes/mytheme/commands.php file

if ( ! class_exists( '\WP_CLI' ) ) {
    return;
}

class Update_All_Posts_Command {

    public function __invoke( $args, $assoc_args ) {
        $all_posts = get_posts( array(
            'post_type'    => 'post',
            'numberposts'  => -1,
        ) );

        foreach ( $all_posts as $single_post ) {
            $single_post->post_title = $single_post->post_title . '';
            wp_update_post( $single_post );
            WP_CLI::success( 'Post updated successfully! ID: ' . $single_post->ID );
        }

        WP_CLI::success( 'All posts updated successfully!' );
    }
}

\WP_CLI::add_command( 'update-all-posts', 'Update_All_Posts_Command' );

The code above is an enhancement from this answer which addresses the memory issue.

Finally, add this to your functions.php file:

require_once get_template_directory() . '/commands.php';

You can now visit your server's terminal and run the command:

wp update-all-posts

And you will get an output such as this:

enter image description here

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