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

wordpress函数add_post_meta()用法示例

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

Add meta data field to a post.


描述

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

更新日志

Version 描述
1.5.0 Introduced.

相关函数

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.

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

热门推荐

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

反馈