admin管理员组

文章数量:1122832

I'm using a free SEO plugin for WordPress called 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.

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.

Share Improve this question edited Mar 5, 2019 at 16:53 JHM asked Mar 5, 2019 at 15:04 JHMJHM 631 gold badge1 silver badge7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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 !!!

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:

本文标签: pluginsHow can I massupdatesave all WordPress posts and pages