下面的代码将帮助您删除除帖子缩略图以外的所有帖子元。
function kv_delete_all_meta_except_featuredimg(){
$args = array( \'posts_per_page\' => -1, \'post_status\' => \'any\', \'post_type\' => array(\'attachment\', \'page\',\'post\'));
$articles= get_posts( $args );
foreach($articles as $article){
if($article->post_type == \'attachment\'){
$myvals = get_post_meta($article->ID);
foreach($myvals as $key=>$val) {
if($key == \'_wp_attached_file\' || $key == \'_wp_attachment_metadata\'){} else {
delete_post_meta($article->ID, $key);
}
}
}else {
$myvals = get_post_meta($article->ID);
foreach($myvals as $key=>$val) {
if($key != \'_thumbnail_id\' ){
delete_post_meta($article->ID, $key);
}
}
}
}
}
add_action(\'init\',\'kv_delete_all_meta_except_featuredimg\');
Note :*** 请记住,这将删除所有post元键及其关联值。因此,在继续操作之前,请先进行备份。只需在主题上添加此功能
functions.php
. 这里我假设你可能只有这些帖子类型
attachment, post, page
. 如果您有任何额外的帖子类型,请添加它。
你可以在这里阅读更多细节,我写了一篇文章。Kvcodes