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

Registers a meta key for posts.

Parameters

$post_typestringrequired
Post type to register a meta key for. Pass an empty string to register the meta key across all existing post types.
$meta_keystringrequired
The meta key to register.
$argsarrayrequired
Data used to describe the meta key when registered. See register_meta() for a list of supported arguments.

Return

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

Source

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

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

Changelog

VersionDescription
4.9.8Introduced.

User Contributed Notes

  1. Skip to note 3 content

    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.