8

I want to check if post meta key exists or not, out side of the loop. Is there any WordPress function to check if post meta key exist or not in WordPress database. I have to check this outside the loop. Any help will be highly appreciated.

26

In case anyone else stumbles across this old question like I just did, it looks like the best way to handle this is using metadata_exists()

see https://developer.wordpress.org/reference/functions/metadata_exists/

Here's what the syntax would be for post meta:

metadata_exists('post', $post_id, 'meta_key_to_check');
| improve this answer | |
2

You could use get_post_custom(POST ID GOES HERE) which will return a multidimensional array with all custom fields of a particular post or page.

So something like:

$meta_data = get_post_custom(785);
if ($meta_data['my_meta_key'][0] != "")
    ...

would allow you check if a particular meta_key was empty or not.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.