0

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...

  • 1
    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
0

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)));
1

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

http://codex.wordpress.org/Class_Reference/WP_Query

  • 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

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.