WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am having trouble figuring out how to pull the meta key values for just one category. I can pull all the meta key values (for meta_key "city") in all categories no problem like this:

 $querystr = "SELECT $wpdb->posts.*,$wpdb->postmeta.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND meta_key = 'city'";

but cannot figure out a way to limit it to a single category. Every time I try to modify the query above with various left and inner and right joins, I get back nothing useful.

Any ideas? Ideally it would filter by the category page we are on, but If I need to create individual category pages, I can deal with that...

share|improve this question
and I guess I wasn't clear what my use case is: Some of these values will be repeating in posts, I just want a non repeating list of the values represented by posts in this category. ie if I have 4 posts with cities NY, SF, LA, and NY, I just want to print out a list of NY, SF, and LA... – Stephen Jul 5 '12 at 21:07

Instead of formulating your own query, why not use WP_Query?

WP_Query( array('cat' => yourcatid, 'meta_key' => 'city') );

This would pull all the keys of City out for a given category id


share|improve this answer
Thanks for the suggestion, but then how do I get this list to stop repeating? For example, if I have two posts with the value "los angeles", I don't want it to be listed twice... – Stephen Jul 5 '12 at 20:53
Are you looking for it to be dynamic or one specific post with certain meta keys? Basically for it to be dynamic or static? – Jonathan Jul 5 '12 at 21:21
dynamic (distinct) listing of all meta values (from meta_key "city") in all posts within the current (or listed) category. – Stephen Jul 5 '12 at 21:52

Apologies, but my PHP isn't wonderful... but I believe for someone who has some at least average php skill, this will make sense. I cut and pasted this line from a query I am doing against a custom taxonomy myself where I only wanted it to display each term only once.

$terms = array_map("unserialize", array_unique(array_map("serialize", $terms)));
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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