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

Adds a meta field to the given post.


Description Description

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


Parameters Parameters

$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


Top ↑

Return Return

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


Top ↑

Source Source

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.
	$the_post = wp_is_post_revision( $post_id );
	if ( $the_post ) {
		$post_id = $the_post;
	}

	return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Hidden Custom Fields

    If you are a plugin or theme developer and you are planning to use custom fields to store parameters related 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.

  2. Skip to note 2 content
    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' );
    }
    
    
  3. Skip to note 4 content
    Contributed by Codex

    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.

You must log in before being able to contribute a note or feedback.