Languages: English • Русский • post meta 日本語 (Add your language)
This function deletes all custom fields with the specified key, or key and value, from the specified post. See also update_post_meta(), get_post_meta() and add_post_meta().
<?php delete_post_meta($post_id, $meta_key, $meta_value); ?>
<?php delete_post_meta(76, 'my_key', 'Steve'); ?>
Let's assume we had 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( $post_meta_key ). This would be added to the "uninstall" function:
<?php delete_post_meta_by_key( 'related_posts' ); ?>
Or, if you wanted to delete all the keys except where post_inspiration was "Sherlock Holmes":
<?php $allposts = get_posts( 'numberposts=-1&post_type=post&post_status=any' ); foreach( $allposts as $postinfo ) { delete_post_meta( $postinfo->ID, 'related_posts' ); $inspiration = get_post_meta( $postinfo->ID, 'post_inspiration' ); foreach( $inspiration as $value ) { if( 'Sherlock Holmes' !== $value ) delete_post_meta( $postinfo->ID, 'post_inspiration', $value ); } } ?>
Or maybe post number 185 was just deleted, and you want to remove all related_posts keys that reference it:
<?php $allposts = get_posts( 'numberposts=-1&post_type=post&post_status=any' ); foreach( $allposts as $postinfo ) { delete_post_meta( $postinfo->ID, 'related_posts', '185' ); } ?>
For a more detailed example, go to the post_meta Functions Examples page.
Note: Unlike update_post_meta(), This function will delete all fields that match the criteria.
delete_post_meta() is located in wp-includes/post.php
Since 1.5.0
Custom Fields: the_meta(), get_post_meta(), add_post_meta(), update_post_meta(), delete_post_meta(), get_post_custom(), get_post_custom_values(), get_post_custom_keys() (See Also: post_meta Function Examples)