admin管理员组

文章数量:1426243

i used ACF to store post data in custom fields. i'd like to copy the data from a wysiwyg field to post_content into wp_posts table. How can i do this?

Roughly:

data is on wp_postmeta table, where meta_key = 'description' and post_id is = $post->ID
and needs to be copied over to 'post_content' of wp_posts table where id is = $post->ID

i used ACF to store post data in custom fields. i'd like to copy the data from a wysiwyg field to post_content into wp_posts table. How can i do this?

Roughly:

data is on wp_postmeta table, where meta_key = 'description' and post_id is = $post->ID
and needs to be copied over to 'post_content' of wp_posts table where id is = $post->ID
Share Improve this question asked May 24, 2019 at 9:40 Michael RogersMichael Rogers 5498 silver badges37 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

First get data from wp_postmeta table

$description = get_post_meta( $post->ID, 'description', true );

Try following code to update content

$my_post = array(
  'ID'           => $post->ID,
  'post_content' => $description,
);

 //Update the post into the database
 wp_update_post( $my_post );

Putting together a working example of @Parthavi Patel answer. This is a page template. When the page loads the code is executed. If there's too many posts it would be good to use a conservative value on posts_per_page and adjust the offset parameter after each batch is executed.

        <?php
/*
 * Template Name: experiments
 * Template Post Type: page
 */

global $post;
$args = array( 
    'post_type' => 'YOUR_CPT_HERE', 
    'posts_per_page' => -1,
    'post_status' => 'publish'
);

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );

    $custom_field_raw = get_field('YOUR_CF_HERE');

    //speedup by skip processing when CF is empty 
    if ($custom_field_raw != '') {

    $my_post = array(
        'ID'           => get_the_ID(),
        'post_content' => $custom_field_raw
    );

    wp_update_post( $my_post );

    }

endforeach; 
wp_reset_postdata();

?>

Reference/Source/Credits: https://support.advancedcustomfields/forums/topic/migrate-a-custom-field-content-to-the_content/

本文标签: databaseHow to copy data from a custom field to the postcontent