I know the question is quite ambiguous. Let me explain:

I have a page that lists all posts with a certain value of a custom field. (Eg.: The page Rentals lists all posts with the value rental of custom field offer_type ).

I'd like to add a filtering option by city, so that only posts with a certain value of city custom field get displayed. For this I created a form using the get method that builds a custom query (in my ex. with two custom fields, 'offer_type' and 'city').

What I need is a way to list all the cities (values of city custom field) for the posts with the rental value of offer_type. Each city has to be listed once.

I tried this bit of code (note that I took all the form wrapping out):

$metakey = 'city';
   $cities = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value 
           FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );

   if ($cities) {   
                 foreach ( $cities as $city ) {
                 echo $city;

but this lists all the values of city custom field for all the posts (so if for eg. I have a post with value of offer_type different than rental, its value of city custom field will be displayed in my list).

I know the issue revolves around sql queries and joining tables, but I really lack the skills to solve this problem.

Please help!

2 Answers 2


take a look at the reference docs for the WP_Query class: http://codex.wordpress.org/Class_Reference/WP_Query

something like this:

$args = array(
  //some key/value pairs...whatever
          'meta_query => array(
                         'key' => somekey,
                         'value' => somevalue,
                         'compare' => some comparison operator
                         'key' => some other key,
                         'value' => some other value,
                         'compare' => some comparison operator

$my_query = new WP_Query( $args );

//some code to do something with the results of the query
  • yes, I am familiar with the meta_query argument. But I don't know how can this help you my problem. I get all the posts I need, but how do I display only once all the values from a certain custom field? I did find an answer for a certain custom post-type here: wordpress.stackexchange.com/questions/9394/… still looking for a solution for the first case (list of all custom field values from posts with a specific value of a different custom field) - I know, doesn't make much sense...
    – rereradu
    Commented Jul 8, 2012 at 15:38
  • Using the meta_query arguments will populate the return object with everything you need to run a custom while have_posts() loop so you don't need to fuss with joining tables etc in your sql.
    – Will
    Commented Jul 8, 2012 at 17:35
  • I tried your suggestion, and it seemed to work just fine until I bumped into another problem. What I did: I set up a custom query with 'meta_query' for two custom fields, I set up an empty array, then during a custom while have_posts() loop I populated it with the value of one of the custom fields. Then I array_unique the array and it seemed like problem solved. Bu then I realized there's a problem with pagination. The array contains only the values present on each page of the query. So I'm stuck again...
    – rereradu
    Commented Jul 8, 2012 at 21:32

this is not a direct answer to your question but a line of code you would use as reference to work onto.

here is a copy of what i use to search thru my table, i search between a data range and then limit it to only display instances of an entered usr_id

 $this->items = $wpdb->get_results($wpdb->prepare("SELECT  *  FROM $table_name WHERE `datec` BETWEEN CAST('$start_date' AS DATE) AND CAST('$end_date' AS DATE) AND `usr_id`= $usr_id"), ARRAY_A);

hope this helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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