This function will delete a specific post meta (custom field) from all posts and pages in WordPress.
See below for a tip and usage example.
PHP
function isa_delete_custom_meta( $meta_key ) {
$args = array(
'post_type' => array_keys( get_post_types( array('public' => true), 'names' ) ),
'posts_per_page' => -1,
'post_status' => array( 'publish', 'draft', 'future' ),
'meta_key' => $meta_key
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
delete_post_meta(get_the_ID(), $meta_key);
}
}
wp_reset_postdata();
}
Tip
This code applies to all pages and post types, including custom post types. If you want to limit it to only regular posts and pages, you can change line 3 above to:
'post_type' => array('post', 'page'),
Usage
For example, to delete a post meta field that has the meta key of MY_CUSTOM_META_KEY, use this:
PHP
isa_delete_custom_meta( 'MY_CUSTOM_META_KEY' );
You must change MY_CUSTOM_META_KEY to your own meta key.
This will delete that post meta field from all posts.
Questions and Comments are Welcome