Function Reference/delete post meta

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn

Description

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().

Usage

%%% <?php delete_post_meta($post_id, $meta_key, $meta_value); ?> %%%

Parameters

tt$post_id/tt
(integer) (required) The ID of the post from which you will delete a field.
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

tt$meta_key/tt
(string) (required) The key of the field you will delete.
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

tt$meta_value/tt
(mixed) (optional) The value of the field you will delete. This is used to differentiate between several fields with the same key. If left blank, all fields with the given key will be deleted.
Default: Emptynoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

Return values

(boolean) 
False for failure. True for success.

Examples

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 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.

Source File

delete_post_meta() is located in onlyincludecodewp-includes/post.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn

Change Log

Since 1.5.0

Related

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)

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude