• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

関数リファレンス/delete post meta

提供: WordPress Codex 日本語版
< 関数リファレンス
2010年4月22日 (木) 21:57時点におけるKz (トーク | 投稿記録)による版 (en:Function Reference/delete post meta 20:44, 7 July 2009 Ramiy)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

Description

This function deletes all custom fields with the specified key from the specified post. See also update_post_meta(), get_post_meta() and add_post_meta().

Usage

%%% <?php delete_post_meta($post_id, $key, $value); ?> %%%

Parameters

$post_id
integer) (必須) The ID of the post from which you will delete a field.
初期値: なし
$key
string) (必須) The key of the field you will delete.
初期値: なし
$value
string) (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.
初期値: なし

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, this would be added to the "uninstall" function:

<?php
  $allposts = get_posts('numberposts=0&post_type=post&post_status=');

  foreach( $allposts as $postinfo) {
    delete_post_meta($postinfo->ID, 'related_posts');
    delete_post_meta($postinfo->ID, 'post_inspiration');
  }
?>

Or, if you wanted to delete all the keys except where post_inspiration was "Sherlock Holmes":

<?php
  $allposts = get_posts('numberposts=0&post_type=post&post_status=');

  foreach( $allposts as $postinfo) {
    delete_post_meta($postinfo->ID, 'related_posts');
    $inspiration = get_post_meta( $postinfo->ID, 'post_inspiration );
    foreach( $inspiration as $value ) {
      if( $value != "Sherlock Holmes" )
        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=0&post_type=post&post_status=');

  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.

Related

カスタムフィールド: 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() (post_meta 関数の例 /en も参照)


関数リファレンステンプレートタグ目次もご覧ください。

最新英語版: WordPress Codex » Function Reference/delete post meta最新版との差分