The below code will help you to remove all the post meta's except the thumbnail of a post.
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 :*** Remember This will remove all the post meta keys and its associated values. So take a backup before proceeding with it. Just add this function on your theme functions.php
. Here i assumed you may have only these post types attachment, post, page
. if you have any extra post types, add with it.
You can read here for more details I wrote an article about it.
Kvcodes