1

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?

Lee
  • 4,187
  • 6
  • 25
  • 71

1 Answers1

4

Generally to check the MetaData exists or not there is a ideal function given by wordpress. i.e :

metadata_exists( string $meta_type, int $object_id, string $meta_key )

The same you are using is correct one i.e :

if(metadata_exists('post', '$post_id', 'example-key')) {
    echo get_post_meta($post_id, 'example-key', true);
}

It determines, if a meta key is set for a given object.

Note : "metadata_exists" function returns directly the bool i.e true or false where as "get_post_meta" function returns the value may be an array id $single is set to "false" or Will return the value if $single is set to "true". So, If as per my understanding the the "metadata_exists" time complexity is less than the other one.

Support Documents for the same : Yes, you can check the following developer docs by wordpress.org (official), you will get the complete understanding of the same. .

1) get_post_meta   : https://developer.wordpress.org/reference/functions/get_post_meta/ 
2) metadata_exists : https://developer.wordpress.org/reference/functions/metadata_exists/

Hope it helps..

Akshay Prabhakar
  • 430
  • 4
  • 13
  • HI, I know what it checks for, that's not what I'm asking, I'd like to know which method is better than the other? – Lee Feb 19 '20 at 15:00
  • @Lee : As per my understanding. "metadata_exists" function returns directly the bool i.e true or false where as "get_post_meta" function returns the value may be an array if $single is set to "false" or Will return the value if $single is set to "true". So, If as per my understanding the the "metadata_exists" timecomplexity is less than the other one. Hope it helps.. – Akshay Prabhakar Feb 19 '20 at 15:13
  • Thanks, yes that is a bit clearer, and is along the lines of what I thought. Do you have any evidence that would support this though? – Lee Feb 20 '20 at 11:37
  • @Lee Yes, you can check the following developer docs by wordpress.org (official), you will get the complete understanding of the same. . 1) get_post_meta : https://developer.wordpress.org/reference/functions/get_post_meta/ 2) metadata_exists : https://developer.wordpress.org/reference/functions/metadata_exists/ Hope it helps, pls do an upvote. I have updated the same in my answer as well. – Akshay Prabhakar Feb 20 '20 at 15:25
  • I'm not going to upvote because you haven't given me any more information that I already have seen or know, I'm looking for evidence that metadata_Exists is preferred over get_meta_data. All you've done is tell me what each one does (which I already knew). – Lee Feb 21 '20 at 16:02