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.
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 edited Nov 8, 2012 at 1:16 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Nov 8, 2012 at 0:41 jasoncjasonc 111 silver badge2 bronze badges2 Answers
Reset to default 2For 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 );
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(
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() ) {
$the_query->the_post();
global $post; // not sure if this is needed, but it can't hurt
echo '
<div style="position: relative;">
<h3 style="display: block;">' .
the_title() . '
</h3>
<div style="width: 48%; display: inline-block; float: none;">' .
the_content() .
'</div>';
$post_counter++;
$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() .
'</div>
</div>';
}
} else {
// no posts found
};
echo
'<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.