delete_post_meta()WP 1.5.0

Remove metadata matching criteria from a post.

You can match based on the key, or key and value. Removing based on key and value, will keep from removing duplicate metadata with the same key. It also allows removing all metadata matching the key, if needed.

No Hooks.

Return

true|false. True on success, false on failure.

Usage

delete_post_meta( $post_id, $meta_key, $meta_value );
$post_id(int) (required)
Post ID.
$meta_key(string) (required)
Metadata name.
$meta_value(mixed)
Metadata value. If provided, rows will only be removed that match the value. Must be serializable if non-scalar.
Default: ''

Examples

0

#1 Demonstration of meta-field removal

// delete all custom fields with the key `my_key`, post 76
delete_post_meta( 76, 'my_key' );

// delete all custom fields `my_key` with value `Steve`:
delete_post_meta( 76, 'my_key', 'Steve' );
0

#2 Deleting specified meta-fields (custom fields)

Suppose we were using a plugin that added custom fields to every post, and now we've abandoned that plugin and need to remove all custom fields that were created by that plugin.

The plugin created fields with the keys keys related_posts and post_inspiration

To remove all these custom fields, you can use this code:

$allposts = get_posts( 'numberposts=-1&post_type=post&post_status=any' );

foreach( $allposts as $postinfo) {
	delete_post_meta( $postinfo->ID, 'related_posts' );
	delete_post_meta( $postinfo->ID, 'post_inspiration' );
}
0

#3 Removing custom fields with exceptions

Suppose you want to remove all custom fields post_inspiration except those with value equal to Sherlock Holmes, use this code:

$allposts = get_posts( 'numberposts=-1&post_type=post&post_status=any' );

foreach( $allposts as $postinfo ) {
	$inspiration = get_post_meta( $postinfo->ID, 'post_inspiration' );

	foreach( $inspiration as $value ) {

		if( $value != 'Sherlock Holmes' ){
			delete_post_meta( $postinfo->ID, 'post_inspiration', $value );
		}
	}
}
0

#4 Example of deleting all meta-fields in a certain post

Option 1:

$post_id = 25;
foreach ( get_post_meta( $post_id ) as $key => $val ) {
	delete_post_meta( $post_id, $key );
}

Variant 2 (faster - one request and no extra operations):

$post_id = 25;
global $wpdb;
$wpdb->delete( $wpdb->postmeta, [ 'post_id'=>$post_id ] );
clean_post_cache( $post_id );
0

#5 Other examples

More examples see here.

0

#6 Delete all meta keys for all posts by meta key name

Let’s assume we have a plugin that added some meta values to posts, but now when we are uninstalling the plugin, we want to delete all the post meta keys that the plugin added. Assuming the plugin added the keys related_posts and post_inspiration.

To delete all the keys use delete_post_meta_by_key(). This would be added to the “uninstall” function:

<?php 
delete_post_meta_by_key( 'related_posts' ); 
delete_post_meta_by_key( 'post_inspiration' );
?>

Changelog

Since 1.5.0 Introduced.

delete_post_meta() code WP 6.1.1

function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
	// Make sure meta is deleted from the post, not from a revision.
	$the_post = wp_is_post_revision( $post_id );
	if ( $the_post ) {
		$post_id = $the_post;
	}

	return delete_metadata( 'post', $post_id, $meta_key, $meta_value );
}