delete_post_meta( int $post_id, string $meta_key, mixed $meta_value = '' )
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 key, if needed.
参数
- $post_id
-
(int)
(Required)
Post ID. - $meta_key
-
(string)
(Required)
Metadata name. - $meta_value
-
(mixed)
(Optional)
Metadata value. Must be serializable if non-scalar.Default value: ”
返回值
(bool) True on success, false on failure.
源代码
File: wp-includes/post.php
function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
// Make sure meta is added to the post, not a revision.
if ( $the_post = wp_is_post_revision($post_id) )
$post_id = $the_post;
return delete_metadata('post', $post_id, $meta_key, $meta_value);
}
更新日志
Version | 描述 |
---|---|
1.5.0 | Introduced. |
相关函数
Uses
-
wp-includes/revision.php:
wp_is_post_revision() -
wp-includes/meta.php:
delete_metadata()
Used By
-
wp-includes/class-wp-customize-nav-menus.php:
WP_Customize_Nav_Menus::save_nav_menus_created_posts() -
wp-admin/custom-header.php:
Custom_Image_Header::ajax_header_remove() -
wp-includes/functions.php:
wp_scheduled_delete() -
wp-includes/class-wp-embed.php:
WP_Embed::delete_oembed_caches() -
wp-includes/post.php:
set_post_thumbnail() -
wp-includes/post.php:
delete_post_thumbnail() -
wp-includes/post.php:
wp_check_for_changed_slugs() -
wp-includes/post.php:
wp_delete_attachment() -
wp-includes/post.php:
wp_update_attachment_metadata() -
wp-includes/post.php:
wp_untrash_post_comments() -
wp-includes/post.php:
wp_insert_post() -
wp-includes/post.php:
wp_delete_post() -
wp-includes/post.php:
wp_untrash_post() -
wp-includes/post.php:
update_attached_file() -
wp-includes/nav-menu.php:
wp_update_nav_menu_item()
Show 10 more used by
Hide more used by
User Contributed Notes
-
Skip to note content
You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note
Contributed by
Codex
Default Usage
<?php delete_post_meta(76, 'my_key', 'Steve'); ?>
Other Examples
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 相关函数 _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( '相关函数 _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, '相关函数 _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 相关函数 _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, '相关函数 _posts', '185' );
}
?>
你可能对这些文章感兴趣:
- wordpress函数edit_user()用法示例
- wordpress函数email_exists()用法示例
- wordpress函数endElement()用法示例
- wordpress函数edit_term_link()用法示例
- wordpress函数edit_post_link()用法示例
- wordpress函数edit_tag_link()用法示例
- wordpress函数edit_link()用法示例
- wordpress函数edit_post()用法示例
- wordpress函数edit_comment_link()用法示例
- wordpress函数edit_form_image_editor()用法示例
- wordpress函数edit_bookmark_link()用法示例
- wordpress函数edit_comment()用法示例
- wordpress函数drop_index()用法示例
- wordpress函数dynamic_sidebar()用法示例
如有疑问,请前往问答中心反馈!
反馈