1

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;
}
0

2 Answers 2

2

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.

4
  • 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, 2013 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, 2013 at 16:42
  • All the posts who have city taxonomy in $cities = array(23,34,45,56).
    – Marius
    Jan 16, 2013 at 17:20
  • Edited the code accordingly.
    – Derk-Jan
    Jan 16, 2013 at 17:28
2

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

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.