Skip to content

Instantly share code, notes, and snippets.

@bpmore
Last active January 29, 2021 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpmore/431e34cb4f7e1d6b8682d55c20bcc670 to your computer and use it in GitHub Desktop.
Save bpmore/431e34cb4f7e1d6b8682d55c20bcc670 to your computer and use it in GitHub Desktop.
Delete All Post Meta Except Thumbnail In WordPress Pages and Posts
//Originally found here: https://www.kvcodes.com/2015/12/delete-all-post-meta-except-thumbnail-in-wordpress-posts/
//Copy from line 3 to the end and paste into your theme's function file.
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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment