返回介绍

delete_post_meta()

发布于 2017-09-10 22:06:52 字数 4873 浏览 1063 评论 0 收藏 0

delete_post_meta( int $post_id,  string $meta_key,  mixed $meta_value = '' )

Remove metadata matching criteria from a post.


description

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);
}

更新日志

Versiondescription
1.5.0Introduced.

相关函数

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

  1. 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'); ?>
    
    
  2. 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' );
      }
    ?>
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文