假设您希望对所有具有custom_field
不为空,将其添加到函数中。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() ;
}
代码获取所有具有
custom_field
并用该字段的内容替换内容。现在,它不会保存更改,只会并排显示旧内容和新内容,因此您可以检查是否一切正常(顺便说一句,在此之前,您应该备份您的db)。
当您准备好查看一些更改时,请取消注释wp_update_post(...)
线
如果您的帖子太多,请将限制改为40-80篇,并取消对delete_post_meta(...)
行,因此自定义字段在复制后会被删除,并且在下次运行函数时不会显示这些字段。
现在你要做的就是[update-posts-from-custom-fields]
在任何帖子或页面中刷新。我自己会把它放在一个空页面中(我通常在我的网站上保留一个测试页面,只对管理员可用,在那里我测试东西并运行类似这样的功能)。
这可能需要一段时间,因为它将呈现它找到的所有帖子。