Up until recently, I've always used the following method to check if a post meta exists:
if(get_post_meta($post_id, 'example-key', true)) {
echo get_post_meta($post_id, 'example-key', true);
}
This of course provides a fallback if the key doesn't exist, but it also calls the same function twice. Is there any benefit of using this instead?
if(metadata_exists('post', '$post_id', 'example-key')) {
echo get_post_meta($post_id, 'example-key', true);
}
It seems that get_post_meta()
will return the value of the key, and metadata_exists()
will return a boolean. In terms of PHP performance, is one faster or more efficient than the other?
Should I stop using the first example, and use the second from now on?