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.

How can I have to set tax_query to get results like 'category__in' => array()?

Specifically, I would like to show all the posts that have a term from the city taxonomy with one of these id's: $cities = array(23,34,45,56);

This is the code I currently use.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
    'meta_query' => array(
    array(
            'key' => 'mar_bedrooms',
            'value' => $bedroomss,
            'compare' => 'IN'
        ),
    array(
            'key' => 'mar_property_for',
            'value' => $property_forss,
            'compare' => 'IN'
        ),
    array(
        'key' => 'mar_price',
        'value' => array( $price_min, $price_max ),
        'type' => 'numeric',
        'compare' => 'BETWEEN'
        )
    ),
    'post_status'=>'publish',
    'post_type'=>'post',
    //'cat' => 1,
    'category__in'=> $type,
    //'taxonomy__in'=> $city,
    'orderby'=>'date',
    'order'=>'DESC'
);
query_posts($args);

if ( have_posts() ) {
    the_post();
    $a = 1;
}
share|improve this question
add comment

2 Answers

You can't, because of the way categories and general taxonomies operate. Categories are a type of taxonomy, so we are querying one level lower when querying for categories. When you query for category__in => array() it actually looks up what category_terms are queried and queries posts from all those categories. Now this effect we can mimic.

$terms_in = array(23,34,45,56);
$taxonomy_terms = get_terms( array( 'city' ), array(
    'orderby' => 'none',
    'hide_empty' => 1,
    'include' => $terms_in
) );

foreach ( $taxonomy_terms as $term ) :
    $args = array(
        'taxonomy' => $term->slug,
        'post_status' => 'publish',
        'posts_per_page' => -1,
    );

    $term_name = $term->slug;
    $query = new WP_Query( $args );
    while( $query->has_posts() ) : 
        $query->the_post();
        // DISPLAY HERE
    endwhile;

endforeach;

wp_reset_postdata();

The code above was edited for your specific question.

share|improve this answer
    
I registered a new taxonomy city, in frontend of site i have a search form who filters by metakeys, category, and taxonomy city. So i have to show posts that match that filters also if i have to list post from one city is simple but if i have to list all cities then i have to use taxonomy__in, but of course isn't available. –  Marius Jan 16 '13 at 15:27
    
@Marius Ah. Do you want to list all the cities? Or all the posts in all the cities? –  Derk-Jan Jan 16 '13 at 16:42
    
All the posts who have city taxonomy in $cities = array(23,34,45,56). –  Marius Jan 16 '13 at 17:20
    
Edited the code accordingly. –  Derk-Jan Jan 16 '13 at 17:28
add comment

After 3 days of research i found the good way to simulate taxonomy_in.

      $args = array(
        'post_type' => 'post',
        'meta_query' => array(
            array(
                'key' => 'mar_bedrooms',
                'value' => $bedroomss,
                'compare' => 'IN'
            ),
            array(
                'key' => 'mar_bathrooms',
                'value' => $bathroomses,
                'compare' => 'IN'
            ),
            array(
                'key' => 'mar_property_for',
                'value' => $property_forss,
                'compare' => 'IN'
            ),
            array(
                'key' => 'mar_price',
                'value' => array( $price_min, $price_max ),
                'type' => 'numeric',
                'compare' => 'BETWEEN'
            )
        ),
        'tax_query' => array(
            array(
                'taxonomy' => 'city',
                'field' => 'id',
                'terms' => $city,  //$city can be also an array
                'operator' => 'IN'
            )
        ),
        'post_status'=>'publish',
        'category__in'=> $type,  //$type can be also an array
        'orderby'=>'date',
        'order'=>'DESC'
    );

    $the_query = new WP_Query( $args );
        if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
wp_reset_query();
wp_reset_postdata();
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.