^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数delete_post_meta()用法示例

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

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈