Using WordPress ‘registered_meta_key_exists()’ PHP function

The registered_meta_key_exists() WordPress PHP function checks if a meta key is registered for a specific object type and its subtype.

Usage

registered_meta_key_exists( $object_type, $meta_key, $object_subtype )

Example:
Input: registered_meta_key_exists( 'post', 'my_custom_meta_key' )
Output: true or false

Parameters

  • $object_type (string) - Required. Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.
  • $meta_key (string) - Required. Metadata key.
  • $object_subtype (string) - Optional. The subtype of the object type. Default: ''.

More information

See WordPress Developer Resources: registered_meta_key_exists()

Examples

Check if a post meta key is registered

Check if the 'my_custom_meta_key' is registered for the 'post' object type.

if ( registered_meta_key_exists( 'post', 'my_custom_meta_key' ) ) {
    echo '**my_custom_meta_key** is registered!';
} else {
    echo '**my_custom_meta_key** is not registered.';
}

Check if a user meta key is registered

Check if the 'user_favorite_color' is registered for the 'user' object type.

if ( registered_meta_key_exists( 'user', 'user_favorite_color' ) ) {
    echo '**user_favorite_color** is registered!';
} else {
    echo '**user_favorite_color** is not registered.';
}

Check if a term meta key is registered

Check if the 'term_icon' is registered for the 'term' object type.

if ( registered_meta_key_exists( 'term', 'term_icon' ) ) {
    echo '**term_icon** is registered!';
} else {
    echo '**term_icon** is not registered.';
}

Check if a comment meta key is registered

Check if the 'comment_rating' is registered for the 'comment' object type.

if ( registered_meta_key_exists( 'comment', 'comment_rating' ) ) {
    echo '**comment_rating** is registered!';
} else {
    echo '**comment_rating** is not registered.';
}

Check if a custom object type meta key is registered

Check if the 'custom_metadata' is registered for the 'custom_object_type' object type and its subtype 'custom_subtype'.

if ( registered_meta_key_exists( 'custom_object_type', 'custom_metadata', 'custom_subtype' ) ) {
    echo '**custom_metadata** is registered for custom_subtype!';
} else {
    echo '**custom_metadata** is not registered for custom_subtype.';
}

Tagged in

Leave a Comment

Your email address will not be published. Required fields are marked *