问题
I currently have a checkbox based search and filter on my wordpress site.
Basically it works using this wp_query
$queryObject = new WP_Query(array("post_type"=>'toy','posts_per_page'=>999999, 'category__and' => $_POST['myListOfCategories']));
I want to move away from using categories (as it's making using the blog a pain)
So I've set everything up using custom taxonomies now and notice there is no "taxonomy__and".
Does anyone know of a way to use WP_Query to search for posts using taxonomies in the same way category__and and works?
e.g. I pass multiple taxonomy id's and it only returns posts which have all of them linked.
回答1:
From http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
You should be able to use something like this;
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => array( 'action', 'comedy' )
),
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
回答2:
You have pass multiple taxonomy in Query_posts in Wordpress
See below URL It is very help full to you:-
https://wordpress.stackexchange.com/questions/25999/how-to-pass-url-parameters-for-advanced-taxonomy-queries-with-multiple-terms-for
https://wordpress.stackexchange.com/questions/10713/wp-3-1-getting-tax-query-to-work-in-query-posts
Query multiple custom taxonomy terms in Wordpress 2.8?
or try it
query_posts( array(
'tax_query' => array(
array(
'taxonomy' => 'tax1',
'field' => 'slug',
'terms' => array('term1', 'term2'),
'operator' => 'OR'
),
array(
'taxonomy' => 'tax2',
'field' => 'slug',
'terms' => array('term3', 'term4'),
'operator' => 'AND'
),
) );
回答3:
With the help of the other posters here is the final answer to my question:
$queryObject = new WP_Query(array("post_type"=>'toy','posts_per_page'=>10,'tax_query' => array( array( 'taxonomy' => 'toy_cats', 'field' => 'id', 'terms' => array(14,20,39,42), 'operator' => 'AND' ) )));
The above code will only show posts that have the "toy" post type, is in the taxonomy "toy_cats" and is is assigned to ALL the following term id's 14 AND 20 AND 39 AND 42
Hope this helps someone else.
来源:https://stackoverflow.com/questions/12250957/wordpress-query-posts-taxonomy