Sometimes in wordpress, we need to know whether a meta key for a post/page exists or not. It may have any value or blank value but we need to check whether it is already created or not. Similarly, someone may also need to know whether a usermeta or comment’s meta exists or not. Earlier it could be checked via firing wp-query only but now it can be checked simply via using a wordpress function which is metadata_exists, such as :
metadata_exists($meta_type, $object_id, $meta_key)
Here $meta_type could be ‘post’ or ‘user’ or ‘comment’ whose key we want to check
$object_id is post’s or user’s or comment’s id whose key we want to check
and $meta_key is the key, we want to check.
It returns true if key exists and false if it does not.
For e.g: if I want to check whether a key _liked exists or not for a post then I will use meta_data as :
$does_exist = metadata_exists(‘post’,$post_id,’_liked’);
So, now you have not to write a big wp query to check whether a meta key exists or not, and the great thing is that using this one function only you can check key of user or comment also.