返回介绍

add_post_meta()

发布于 2017-09-10 21:16:01 字数 5349 浏览 1288 评论 0 收藏 0

add_post_meta( int $post_id,  string $meta_key,  mixed $meta_value,  bool $unique = false )

Add meta data field to a post.


description

Post meta data is called "Custom Fields" on the Administration Screen.


参数

$post_id

(int) (Required) Post ID.

$meta_key

(string) (Required) Metadata name.

$meta_value

(mixed) (Required) Metadata value. Must be serializable if non-scalar.

$unique

(bool) (Optional) Whether the same key should not be added.

Default value: false


返回值

(int|false) Meta ID on success, false on failure.


源代码

File: wp-includes/post.php

function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
	// 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 add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
}

更新日志

Versiondescription
1.5.0Introduced.

相关函数

Uses

  • wp-includes/revision.php: wp_is_post_revision()
  • wp-includes/meta.php: add_metadata()

Used By

  • wp-includes/theme.php: _wp_customize_publish_changeset()
  • wp-includes/post.php: wp_add_trashed_suffix_to_post_name_for_post()
  • wp-admin/includes/image.php: wp_generate_attachment_metadata()
  • wp-admin/includes/post.php: wp_write_post()
  • wp-admin/includes/post.php: add_meta()
  • wp-includes/functions.php: do_enclose()
  • wp-includes/post.php: _publish_post_hook()
  • wp-includes/post.php: wp_check_for_changed_slugs()
  • wp-includes/post.php: wp_insert_post()
  • wp-includes/post.php: wp_trash_post()
  • wp-includes/post.php: wp_trash_post_comments()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::add_enclosure_if_new()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::set_custom_fields()
  • Show 8 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: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    Adding or Updating a Unique Custom Field
    Adds a new custom field if the key does not already exist, or updates the value of the custom field with that key otherwise.

    
    <?php
    if ( ! add_post_meta( 7, 'fruit', 'banana', true ) ) { 
       update_post_meta ( 7, 'fruit', 'banana' );
    }
    
    
  2. Hidden Custom Fields

    If you are a plugin or theme developer and you are planning to use custom fields to store parameters 相关函数 to your plugin or template, it is interesting to note that WordPress will not show custom fields which have keys starting with an “_” (underscore) in the custom fields list on the post edit screen or when using the the_meta() template function. This can be for example used to show these custom fields in an unusual way by using the add_meta_box() function.

    The following example:

    
    <?php add_post_meta( 68, '_color', 'red', true ); ?>
    

    will add a unique custom field with the key name _color and the value ‘red’ but this custom field will not display in the post edit screen.

    In addition, if the $meta_value argument is an array, it will not be displayed on the page edit screen, even if you don’t prefix the key name with an underscore.

    Default Usage

    
    <?php add_post_meta( 68, 'my_key', 47 ); ?>
    
    

    Other Examples
    Adds a new custom field only if a custom field with the given key does not already exists:

    
    <?php add_post_meta( 68, 'my_key', '47', true ); ?>
    

    Adds several custom fields with different values but with the same key ‘my_key’:

    
    <?php add_post_meta( 68, 'my_key', '47' ); ?>
    <?php add_post_meta( 68, 'my_key', '682' ); ?>
    <?php add_post_meta( 68, 'my_key', 'The quick, brown fox jumped over the lazy dog.' ); ?>
    

    For a more detailed example, see the post_meta Functions Examples page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文