下面是一个如何获取元数据并将其设置为帖子内容的示例。您需要将所有元键添加到$meta_fields
大堆
显然,在生产环境中运行之前,请在本地进行测试,因为它将覆盖帖子内容中当前的内容。
function migrate_post_data() {
// Add all the meta keys you want to migrate to this array.
$meta_fields = array(
\'flexible_content_1_content\',
\'flexible_content_2_content\'
);
// Get all the posts.
$args = array(
\'post_type\' => \'post\', // You may need to change this.
\'posts_per_page\' => 400,
);
$posts = get_posts( $args );
// Loop over each post.
foreach ( $posts as $post ) {
$meta_field_content = array();
// Loop over each meta field and get their content, adding it to an array.
foreach( $meta_fields as $meta_field ) {
$content = get_post_meta( $post->ID, $meta_field, true );
if ( ! empty( trim( $content ) ) ) {
$meta_field_content[] = $content;
}
}
if ( empty( $meta_field_content ) ) {
continue;
}
// Set the the post content as whatever the meta fields were.
$post_args = array(
\'ID\' => $post->ID,
\'post_content\' => implode( \' \', $meta_field_content ),
);
wp_update_post( $post_args );
}
}