I’ve got two taxonomies: “type of guide”, and “tools”.
They look something like this:
Type of guide
- Article
- Video
- Cheat sheet
Tool
â Learning Management System
-
Quiz Tool
- Hide a grades column (article) - Creating a Gradebook (article) - Long Answer Question Steps (article) - Matching Questions (article) - True or False (article) - Using the Quiz Tool (video) - Using the Quiz Tool (cheat sheet)
-
Notifications tool
- Grades Tool
The screenshot outlines the problem perfectly: I need to limit that tax-query to include only items that are tagged with “Quiz Tool”.
Screen: http://cl.ly/0t3J143q0A1X2w2y2X2n
I’m trying to do it with a tax_query, with an AND relation:
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'guide-type',
'field' => 'slug',
'terms' => 'video'
),
array(
'taxonomy' => 'tool',
'field' => 'slug',
'terms' => what do I put here to get the current tool (Quiz tool)?
)
)
);
$query = new WP_Query( $args );
But I just can’t figure out how to limit to only list items that are tagged with “Quiz Tool”. Note the, “what do I put here” part.
Help appreciated.
UPDATE:
It just occurred to me that I don’t have to query both taxonomies, instead exit the tax_query and use a ‘get_the_category()’ as part of the $args:
// The Query
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'guide-type',
'field' => 'slug',
'terms' => 'self-paced '
)
),
'category_name' => get_the_category()
Something like that? Any ideas how I could limit that ‘category_name’ to be the current one I’m looking at (Quiz Tool).
Terry
This helper function will return the ID of the term you are currently viewing (from any taxonomy – you can limit just to a specific taxonomy, or the built-in tag and category taxonomies).
It will return false if you are not on a taxonomy term archive page.
Then you can do something like the following:
Note: I’ve not tested this
This should get all post data for the taxonomy “Tool” with term “Quiz Tool”.
Alternate way to do this is to use
get_the_terms()
to figure out what tax term page you’re on, then pass that to thetax_query
.Now I can pass
$current_term
into my tax_query:I’m returning the last item in the array, because that’s tax page I’m on. You could return the value from any position in the array you wanted. I happened to want the last one and so used
end(array)
.