getting all values for a custom field key (cross-post)

Written by  on September 13, 2016 

I know how to get a custom field value for a specific post.

get_post_meta($post_id, $key, $single);

What I need is to get all the values associated with a specific custom post key, across all posts.

Anyone knows of an efficient way to do this? I wouldn’t want to loop through all post id’s in the DB.


4 posts all with different values for a custom field called ‘Mood’.
2 posts have the value ‘happy’, 1 post have ‘angry’ and 1 post has ‘sad’

I want to output : across all posts we have: two happy, one angry and one sad author(s).

But for LOTS of posts.

What I’m looking for is either:

  • a WP function to get this.
  • a custom query to get this as efficiently as possible.

Category : Wordpress

Tags :