register_post_meta( string $post_type, string $meta_key, array $args ): bool

Registers a meta key for posts.


$post_type string Required
Post type to register a meta key for. Pass an empty string to register the meta key across all existing post types.
$meta_key string Required
The meta key to register.
$args array Required
Data used to describe the meta key when registered. See register_meta() for a list of supported arguments.
More Arguments from register_meta( ... $args ) Data used to describe the meta key when registered.
  • object_subtype string
    A subtype; e.g. if the object type is "post", the post type. If left empty, the meta key will be registered on the entire object type. Default empty.
  • type string
    The type of data associated with this meta key.
    Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
  • description string
    A description of the data attached to this meta key.
  • single bool
    Whether the meta key has one value per object, or an array of values per object.
  • default mixed
    The default value returned from get_metadata() if no value has been set yet.
    When using a non-single meta key, the default value is for the first entry.
    In other words, when calling get_metadata() with $single set to false, the default value given here will be wrapped in an array.
  • sanitize_callback callable
    A function or method to call when sanitizing $meta_key data.
  • auth_callback callable
    Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.
  • show_in_rest bool|array
    Whether data associated with this meta key can be considered public and should be accessible via the REST API. A custom post type must also declare support for custom fields for registered meta to be accessible via REST.
    When registering complex meta values this argument may optionally be an array with 'schema' or 'prepare_callback' keys instead of a boolean.

Top ↑


bool True if the meta key was successfully registered, false if not.

Top ↑


File: wp-includes/post.php. View all references

function register_post_meta( $post_type, $meta_key, array $args ) {
	$args['object_subtype'] = $post_type;

	return register_meta( 'post', $meta_key, $args );

Top ↑


Version Description
4.9.8 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by shramee

    If you are wondering why it doesn’t work for your custom post type when doing a block editor plugin or something,
    Your custom post type needs to support custom-fields.

        register_post_type( 'book', array(
        	'supports' => array( 'title', 'editor', 'custom-fields', ..., ),
        	// Make sure you add custom-fields here ^^^^^^^^^^^^^
        ) );

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