Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer

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(
                          array(
                         'key' => somekey,
                         'value' => somevalue,
                         'compare' => some comparison operator
                         ),
                         array(
                         '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
share|improve this answer
    
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 Jul 8 '12 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 the Web Mechanic Jul 8 '12 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 Jul 8 '12 at 21:32
add comment

Your Answer

 
discard

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.