Skip to content
Advertisement

Bulk move 130,000 post content value to a custom Filed in wordpress

My current blog already has 130,000 pieces of data, and the content of each of my articles is a number.The content body of all articles is a number。

I want to batch transfer the content field value to a new field named ‘views’.

I used the method of this tutorial

Never succeed with below message Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 20480 bytes) in /usr/local/lighthouse/softwares/wordpress-plugin/wp-includes/class-wp-term.php on line 198

After that I modified the memory size in the php.ini and wp-config.php files, but still not working.

blow is the code I used in function.php

add_action( 'get_header', 'so19967768_update_posts' );
function so19967768_update_posts()
{
    if( ! isset( $_GET['update'] ) )
        return;

    $posts = get_posts( array( 'post_type' => 'post', 'posts_per_page' => -1 ) );

    foreach( $posts as $post )
    {
        setup_postdata( $post );
        $value = $post->post_content;
        update_post_meta( $post->ID, 'views', $value );
        wp_update_post( array(
            'ID' => $post->ID,
            'post_content' => ''
        ) );
    }

    echo 'update complete!';

    die();
}

Advertisement

Answer

You must do that by MySQL query like this in wp function:

add_action( 'get_header', 'so19967768_update_posts' );
function so19967768_update_posts()
{
    if( ! isset( $_GET['update'] ) )
        return;

    global $wpdb;
    $wpdb->query( "INSERT INTO `wp_postmeta`(`post_id`, `meta_key`, `meta_value`) SELECT ID,'views',post_content FROM `wp_posts`;");
    $wpdb->query( "UPDATE `wp_posts` SET `post_content`='';");

    die();
}

Also, You can run this directly on wp_postmeta table in database:

INSERT INTO `wp_postmeta`(`post_id`, `meta_key`, `meta_value`) SELECT ID,'views',post_content FROM `wp_posts`;
UPDATE `wp_posts` SET `post_content`='';

*Trick: If wp function doesn’t work by load website, comment this line:

if( ! isset( $_GET['update'] ) )
        return;
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement