delete_post_meta() - WordPress 函数参考中文文档

返回介绍

delete_post_meta()

发布于 2017-09-10 字数4895 浏览 931 评论 0

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!