Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to move (or copy) the contents of a custom field into the main post body, replacing any existing content.

I need to do this for a few hundred posts...Is there a quick way of doing this?

I can manage moving values from one meta field to another in SQL, but i'm not sure how to do this with post content...

If anyone could give me some pointers that would be much appreciated.

share|improve this question

2 Answers 2

For SQL, the post's content is saved in "wp_posts" table under "post_content" column. When doing this from phpmyadmin, you'll have to take care that you're updating only the main post & not one of it's revisions

If you're looking for some wordpress based solution, the wordpress way to do it is to use wp_update_post

// Update post 37
  $my_post = array();
  $my_post['ID'] = 37;
  $my_post['post_content'] = get_post_meta($my_post['ID'], 'meta_key', true);

// Update the post into the database
  wp_update_post( $my_post );
share|improve this answer
Thanks Mridul, yeah there's all sorts of different content types under wp_posts as well so i'm avoiding doing it this way to be safe. I shall investigate the wordpress way that you suggested. – jasonc Nov 9 '12 at 1:51

Assuming you want to do this for all posts that have the custom_field not empty, add this to your functions.php:

add_shortcode('update-posts-from-custom-fields', 'upfc_fields321');
function upfc_fields321() {
    $args = array(
        'meta_key' => 'custom_field',
        'meta_query' => array(
                'key'     => 'custom_field',
                'value'   => '',
                'compare' => '!=',
        'post_count' => '-1'
    $the_query = new WP_Query( $args );

    if ( $the_query->have_posts() ) {
        $post_counter = $save_counter = $delete_counter = 0;
        while ( $the_query->have_posts() ) {
            global $post; // not sure if this is needed, but it can't hurt
            echo '
            <div style="position: relative;">
                <h3 style="display: block;">' .
                the_title() . '
                <div style="width: 48%; display: inline-block; float: none;">' .
                the_content() .

            $post->post_content = get_post_meta($post->ID, 'custom_field', true);
            $post->post_content_filtered = '';
            $post->post_excerpt = '';

            // uncomment next line when you are ready to commit changes
            // wp_update_post($post); $save_counter++;

            // uncomment next line if you want to delete the meta key (useful if you have too many posts and want to do them in batchces)
            // delete_post_meta($post->id, 'custom_field'); $delete_counter++;

            echo '
                <div style="width: 48%; display: inline-block; float: none">' .
                the_content() .
    } else {
        // no posts found
        '<hr>Processed posts: ' . $post_counter .
        '<hr>Saved posts:' . $save_counter .
        '<hr>Deleted meta from: ' . $delete_counter . ' posts';
    wp_reset_postdata() ;

The code gets all the posts that have custom_field and replaces the content with the contents of that field. As it is now, it does not save the changes, just displays the old and the new content side by side, so you can check if everything is right (you should backup your db anyways before this, btw).

When you're ready to see some changes, uncomment the wp_update_post(...) line.

If you have too many posts, change the limit to something like 40 - 80 posts and also uncomment the delete_post_meta(...) line, so the custom fields get deleted after being copied and they don't show up in next run of the function.

Now all you have to do is place [update-posts-from-custom-fields] in any post or page and refresh. I myself would put it in an empty page (i usually keep a test page on my websites, only available for admin, where I test stuff and run functions like this one).

It might take a while, as it will render all the posts it finds.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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