WordPress函数文档delete_post_meta()
删除文章/页面的自定义栏目(字段) 描述 译文 该函数从指定文章中删除含有指定关键字的所有自定义字段。参见up…
删除文章/页面的自定义栏目(字段)
描述
译文
该函数从指定文章中删除含有指定关键字的所有自定义字段。参见update_post_meta(), get_post_meta() 和add_post_meta()。
原文
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().
用法
<?php delete_post_meta($post_id, $meta_key, $meta_value); ?>
参数
$post_id
(integer) (必填) The ID of the post from which you will delete a field.
默认值: None
$meta_key
(string) (必填) The key of the field you will delete.
默认值: None
$meta_value
(mixed) (可选) 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.
默认值: Empty
示例
Default Usage
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?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:
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?php delete_post_meta_by_key( ‘related_posts’ ); ?>
|
Or, if you wanted to delete all the keys except where post_inspiration was “Sherlock Holmes”:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?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:
1
2
3
4
5
6
7
8
9
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?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.
历史
添加于 版本 1.5.0
源文件
delete_post_meta() 函数的代码位于 wp-includes/post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* 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.
*
* @since 1.5.0
*
* @param int $post_id Post ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Optional. Metadata value. Must be serializable if
* non-scalar. Default empty.
* @return bool True on success, false on failure.
*/
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);
}
|
相关
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)
- 原文:http://codex.wordpress.org/Function_Reference/delete_post_meta
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!