1

Ok, so I have a custom post type (let's call it 'custompost') with about 3,000 posts and each has about 150 advanced custom field data items. I am trying to update the post_content for all of these by way of a plug-in I made:

<?php

/**
 * Plugin Name:       Bulk Post Update
 * Version:           1.0.0
 **/

add_action('init','bulk_post_update');

function bulk_post_update(){

$my_posts = get_posts( array('post_type' => 'custompost', 'posts_per_page' => -1 ) );

foreach ( $my_posts as $my_post ):

$my_post->post_content = 'Text I want to update all my post content to';
wp_update_post( $my_post );

endforeach;
}

?>

It works fine if I just use some small value like 'posts_per_page' => 50, but if I try to use -1 for all posts, then I think there are memory issues as my site will just come up blank.

Anyway to get this to working? Either by increasing the memory or some other method?

Maybe somehow running this in batches of 50? How would I go about doing such a thing in a non-tedious way?

Any help/suggestions welcome.

Many thanks in advance.

0
<?php

/**
 * Plugin Name: Bulk Post Update
 * Version:     1.0.0
 *
 * @author Nabil Kadimi <nabil@kadimi.com>
 * $link   http://wordpress.stackexchange.com/a/242499/17187
 **/

add_action( 'init', function() {

    global $wpdb;

    $table            = $wpdb->posts;
    $post_type        = 'custompost';
    $new_post_content = 'Text I want to update all my post content to';

    $affected = $wpdb->query(
        $wpdb->prepare( "UPDATE {$table} SET post_content = %s, WHERE post_type = %s"
            , $new_post_content
            , $post_type
        )
    );

    echo $affected . ' rows modified, please disable code.' . "\n";
    echo 'File: ' . __FILE__ . "\n";
    echo 'Line: ' . __LINE__ . "\n";
    die();
} );

Edit

Based on our discussion, I believe overriding content on the fly with code will be less intensive on PHP and MySQL, here is how I would do it:

<?php

/**
 * Plugin Name: Override Post Content
 * Version:     1.0.0
 *
 * @author Nabil Kadimi <nabil@kadimi.com>
 * $link   http://wordpress.stackexchange.com/a/242499/17187
 **/

add_filter( 'the_content', function( $content ) {

    global $post;
    $custom text = 'blah [blah]';

    /**
     * Ignore other post types.
     */
    if ( 'custompost' !== $post->post_type ) {
        return $content;
    }

    /**
     * Return the value I want to override with.
     */
    return do_shortcode( $custom_text );
} );
  • So this is an SQL approach to the problem? – joeyjoejoejrshabidu Oct 13 '16 at 10:23
  • Before I try it, is there any limit the size of $new_post_content I can use? I want to use something with about 100,000 characters in it. – joeyjoejoejrshabidu Oct 13 '16 at 10:25
  • Yes, this is on a lower lever (SQL VS WordPress API). And no, there is no known limit except for the MySQL maximum for longtext which is 4GB. On a side note, if all your posts will have the same content, why not ignore there current content and have a plugin deliver the content, It will be stored once and you will save your database at least 300MB of useless clutter. – Nabil Kadimi Oct 13 '16 at 10:37
  • @joeyjoejoejrshabidu Answer updated – Nabil Kadimi Oct 13 '16 at 10:45
  • Thanks for the help! What plugin? It's not exactly the same content, as it's acf and visual composer shortcode. Would this plugin still work for this? – joeyjoejoejrshabidu Oct 13 '16 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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