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.
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 );
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'
),
) );
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.
Related
I am trying to get custom post types from a custom taxonomy. Following the documentation and other similar problems related here, i did the following:
$query = new WP_Query( array(
'post_type' => 'job',
'tax_query' => array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'california'
)
)
But the problem is that this query is getting all posts, not just posts under "California" taxonomy.
If more information is needed, i can provide more code editing the question.
Thanks in advance!
Try something like this:
$posts_array = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'job',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'california',
)
)
)
);
You're missing an array. The tax query argument can be used for multiple taxonomies. It accepts an array of arrays.
Important Note: tax_query takes an array of tax query arguments arrays
(it takes an array of arrays). This construct allows you to query
multiple taxonomies by using the relation parameter in the first
(outer) array to describe the boolean relationship between the
taxonomy arrays.
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Change this:
'tax_query' => array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'california'
)
To this:
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'california',
),
),
In your while loop, remember to switch
<?php
while ( $query->have_posts() ) {
$query->the_post();
// Important line, especially if you have
//multiple WP_Query invocations
DO WHAT YOU WANT HERE
wp_reset_postdata();
}
?>
I am working with WP_Query to pull results from DB using multiple taxonomy from a custom post type.I have two taxonomy with drop down each.Taxonomy city and cuisine but if a dont't select any one of them results don't show.Actually i want to show results only with the key words even if these taxonomies are not selected.
My codes
$args = array(
'post_type' => 'listings',
's' => get_query_var( 's' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'cuisine',
'field' => 'slug',
'terms' => $selected_cuisine,
'relation' => 'AND',
),
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $selected_city,
'relation' => 'AND',
),
),
);
$restaurant_query = new WP_Query( $args );
In what way does your query know when a taxonomy term is selected? You could try to add the tax_query with an if statement later on like this:
if ($term == 'cuisine') {
$args['meta_query'][] = array(
'taxonomy' => 'cuisine',
'field' => 'slug',
'terms' => $selected_cuisine,
'relation' => 'AND',
);
} elsif($term == 'city') {
$args['meta_query'][] = array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $selected_city,
'relation' => 'AND',
);
}
This why the tax_query will only be added when the term is selected. I've had a few strange encounters with this issue in the past and this method fixed my issue quite a few times.
I have a custom post type of image with a custom taxonomy called image_tag (it's hierarchical like categories). Here are some examples of the tags that might be used:
Structure (id: 25)
- House (id: 56)
- Skyscraper
Nature
- Animal
- Plant (id: 41)
So, I want to drill down through the images by selecting multiple tags in conjunction with the "and" operator. For example, finding all photos with plants and houses.
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => 'image_tag',
'terms' => array(41, 56), // IDs of "plant" and "house"
'operator' => 'and',
),
),
);
That works fine, the problem begins when I try to include the parent terms, for example:
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => 'image_tag',
'terms' => array(25, 41), // IDs of "structure" and "plant"
'operator' => 'and',
),
),
);
Then I get no results. I'm guessing that because I'm using the "and" operator, Wordpress doesn't include the children of the "Structure" term. Does anyone have an idea how I can get this to work?
So it should look like this:
'relation' => 'AND',
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array( 25 ),
),
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array( 41 ),
),
),
Update: You forget to close 'terms' and 'operator' by ' like this
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array(25, 41), // IDs of "structure" and "plant"
'operator' => 'in'
),
),
);
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array(25, 41), // IDs of "structure" and "plant"
'operator' => 'in'
),
),
);
I had a similar problem, give this a try!
I have been reading the WP_Query Codex looking for a way to loop through all the posts that have the post format 'video' OR 'image', within a given category.
If this wasn't enough, this category is given by a variable $catslug (I need it to be this way).
I have only found ways of looping through one of the following
image OR video
image AND category
video AND category,
but what I need is more complex, something like this:
post-format-image AND $catslug) OR (post-format-video AND $catslug)
Is it possible to do a tax_query within a tax_query?
Something like this:
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($catslug)
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-image' )
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($catslug)
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video' )
)
)
)
);
$query = new WP_Query( $args );
Anyone knows any workaround or a hack to get this?
Perhaps I'm just thinking the wrong way.
This actually a good question. The simple answer here is that you cannot use multiple tax_query's.
This had me quickly testing the following scenario before I left for work. Just for fun I went and tried to make use of the category parameters with a tax_query, but that gave me posts from the desired category and posts that also belongs to both post formats
I came up with a possible solution, unfortunately I cannot test this right now.
Here is my line of though here:
As you need random results, I would suggest that you add your post formats into an array
$post_format_array = array( 'post-format-video', 'post-format-image' );
You now are going to use shuffle to shuffle the array, and then take the first entry from that array, which will be either video of image, and use that in your tax_query.
shuffle( $post_format_array );
This way you will get posts that is in your desired category and in either video or image post format.
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $catslug,
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $post_format_array[0]
),
),
);
$query = new WP_Query( $args );
I'm using WP_Query (pretty standard). It all works great.
However, I have a particular modification to make, where, if the user enters the specific post name in the URL, the search will return only the post that matches that post_name value.
See my code below with a comment about the particular line not working.
<?php
$getPeople = array(
'post_type' => 'person',
'posts_per_page' => -1,
// I want this below to only return me the post with this specific value.
// This doesn't error, but doesn't work either.
// I know it seems counter-productive to a 'search' but this particular case requires it.
// This has a hard-coded value at the moment.
'post_name' => 'rebecca-atkinson',
'orderby' => 'meta_value',
'meta_key' => 'last-name',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'gender',
'value' => $theGender,
)
),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'accent',
'field' => 'slug',
'terms' => $theAccent,
'operator' => 'IN',
),
array(
'taxonomy' => 'style',
'field' => 'slug',
'terms' => $theStyle,
'operator' => 'IN',
),
array(
'taxonomy' => 'age',
'field' => 'slug',
'terms' => $theAge,
'operator' => 'IN',
),
)
);
$myposts = new WP_Query($getPeople);
?>
Your help would be greatly appreciated. If I could just see how to search on this specific 'slug' then that would be great.
Many thanks,
Michael.
Instead of
'post_name' => 'rebecca-atkinson',
use:
'name' => 'rebecca-atkinson',
In addition to my answer in the comments above, I thought I'd post it as an official answer too:
I have to use 'name' and NOT 'post_name'.
For example:
'name' => 'rebecca-atkinson'
Hope this helps someone in future.