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 am allowing users to query posts using a form with checkboxes. I want the posts to be filtered by the checkboxes but instead of showing all posts that include the ANY of the meta values selected I want to show posts that contain all of the selected values. Is this possible? Here is what I currently have using the "IN" operator which just checks if the post contains any single value in the array.

$the_query = new WP_Query(array(
            'post_type' => 'listing',
            'posts_per_page' => -1,
            'post_status' => 'publish', 
            'category_name' => 'private_rental',
            'orderby' => 'rand',
            'meta_query' => array(
                    'key' => 'row_amenities',
                    'value' => $amenities,
                    'compare' => 'IN'
                    )
        ));
share|improve this question
    
Have you searched the site? I know this has already been answered. I guess it's filed in the meta-query archive. –  kaiser Dec 3 '12 at 20:25
    
I saw some posts that were related but none that actually explicitly answered my question. From what I gathered doing a loop and excluding posts based on their meta is the only solution. –  tyler Dec 3 '12 at 21:12
1  
Ok. Then please tell us what $amenities is exactly: Content + Type (int/float/string/array/etc.) –  kaiser Dec 3 '12 at 21:59
add comment

1 Answer

up vote 1 down vote accepted

You can have multiple meta_query arguments with the same key and that will produce the results you're seeking. Here's an example:

$meta_query = array_map( create_function( '$a', 'return array("key" => "row_amenities", "value" => $a);' ), $amenities );

$the_query = new WP_Query(array(
            'post_type' => 'listing',
            'posts_per_page' => -1,
            'post_status' => 'publish', 
            'category_name' => 'private_rental',
            'orderby' => 'rand',
            'meta_query' => $meta_query
        ));

If $amenities will have a lot of values (> 10), you should probably take a different approach, because it will join the postmeta table for each meta_query.

share|improve this answer
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.