How to Query Posts With Multi Taxonomys

If you like WordPress, or just if you are obliged to use it.
If you use custom post types and custom taxonomys.
Surely you’ve found yourself in this dilemma.

How to query posts with multi taxonomys and diferent values?

The answer is very simples.
You don’t need any plugin, just need to use ‘tax_query’ in the query post.

Tax_query let you define multiple taxonomys.
Setting the taxonomy you want to search, the field, the value of that field and/or the operator.

[php]
$args = array(

‘relation’ => ‘AND’, // Optional
‘tax_query’ => array(
array(
‘taxonomy’ => ‘taxonomy_district’,
‘field’ => ‘id’,
‘terms’ => ‘Oporto’,
// Possible values to operator
// ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’.
‘operator’ => ‘IN’
),
array(
‘taxonomy’ => ‘taxonomy_county’,
‘field’ => ‘slug’,
‘terms’ => ‘Matosinhos’
)
),

‘post_type’ => ‘enterprises’,
‘orderby’ => ‘date’,
‘order’ => ‘ASC’,
‘paged’=> $paged,
‘posts_per_page’ => 12

);

query_posts($args);
[/php]

Share Post :

9 Comments

  • Gaynelle Maruca
    19 de Março de 2012 at 10:02 

    Lovely site! I am loving it!! Will come back again. I am taking your feeds also

  • Wilburn Gadbois
    26 de Março de 2012 at 22:07 

    Very nice post, I certainly love this website, keep on it.

  • Cruz Bosa
    5 de Abril de 2012 at 3:22 

    Fantastic goods from you, man. I’ve understand your stuff previous to and you are simply too excellent. I really like what you’ve acquired here, certainly like what you are stating and the best way in which you are saying it. You are making it entertaining and you still take care of to keep it sensible. I can’t wait to learn far more from you. This is really a terrific site.

  • Everette Moncher
    10 de Abril de 2012 at 0:09 

    you’re in point of fact a excellent webmaster. The website loading speed is incredible. It kind of feels that you’re doing any distinctive trick. In addition, The contents are masterwork. you’ve done a excellent job in this matter!

  • Cliff Bissegger
    10 de Abril de 2012 at 22:11 

    Simply wanna say that this is very beneficial , Thanks for taking your time to write this.

  • Coral Keanum
    10 de Abril de 2012 at 23:30 

    some times its a pain in the ass to read what website owners wrote but this web site is real user friendly ! .

  • Lucius Wiechec
    11 de Abril de 2012 at 0:58 

    wohh precisely what I was searching for, thankyou for putting up.

  • shawn manolas
    11 de Abril de 2012 at 2:32 

    Great piece of info! May I reference part of this on my blog if I post a backlink to this webpage? Thank you.

Hey! comments are closed.