I'm working in a wordpress directory site where I have 1 <strong> 1 custom post type</strong>, called "outfitter", and <strong>2 taxonomies</strong> related to this post type:
- Taxonomy "activity" (terms are: canyoning, adventure, tourism...)
- Taxonomy "destination" (terms are: spain, france...)
For example one<em> outfitter</em> called Vertientes has the <em>activity taxonomy</em> "canyoning" and <em>destination taxonomy</em> Costa Rica".
The taxonomies pages (taxonomy-activity.php, taxonomy-destination) are working correctly. For example:
- for the query <em>?activity=canyoning</em> , are listed the posts (outfitter) with that taxonomy term.
- for the query <em>?destination=costarica</em>, are listed the post (outfitter) with the destination Costa Rica.
Until here, everything works fine.
<strong>But Now it come my question!</strong>
In the "activity" taxonomy page (?activity=canyoning
) I need to list the terms from the "destination" taxonomy which has some outfitter. This terms have the link to filter the outfitter (?activity=canyoning&?destination=costarica
)
Example Page for "Canyoning" taxonomy page:
List of Outfitters with canyoning activity (now is working).
<strong>List of Canyoning Destinations: Spain (1) | France (3) .... => THIS IS WHAT I NEED </strong>
So in the List of Destination I don't show all the terms in it, only the Destinations with also the "canyoning" term.
I've attached an image to be more clear.
<strong>How can I do it?</strong>
It has to work similar than the widget from [[LINK href="http://wordpress.org/extend/plugins/query-multiple-taxonomies/"]]query multiple taxonomies[[/LINK]] but as I just want a simple function to work not a whole plugin.
<strong>Anybody has done something similar?</strong>
John Cotton answers:
If I've understood you correctly you want a list of terms from the second taxonomy that have objects (ie posts/pages/custom types) that are in the first term. Is that correct?
If so, you need to use a combination of [[LINK href="http://codex.wordpress.org/Function_Reference/get_objects_in_term"]]get_objects_in_term[[/LINK]] and [[LINK href="http://codex.wordpress.org/Function_Reference/wp_get_object_terms"]]wp_get_object_terms[[/LINK]].
You use the first to get an array of objects that are limited to the term you are interested.
That's your outer set (think Venn diagram).
Now pass that list to the second function with the appropriate taxonomy restriction to get back just the terms you want.
So some code might look like this:
if( $objects = get_objects_in_term( $term_id, 'activity' ) ) {
if( $secondary_terms = wp_get_object_terms( $objects, 'places' ) ) {
foreach( $secondary_terms as $term ) {
// output as you want...
}
}
}
$term_id is the term for the current page....
Daniel Tomas comments:
Yes, you understand well:
The second taxonomy call 'destination', have objects ('outfitter' custom type) that are in the first term ('canyoning', from 'activity' taxonomy).
Can you help creating a function? I'm not used to get_objects.
Daniel Tomas comments:
I didn't see your function, I'm trying it on the theme, probably will work ;)
Daniel Tomas comments:
Nice solution John, I didn't know these functions.
The last thing is to show the numbers of terms, like in the attachment.. it's possible to do it with that function? Something like "category_count"?
Ex:
- Spain (2) means that 2 outfitter does canyoning in Spain
- Costa Rica (1)
John Cotton comments:
Sadly, you have to make another database call - the downside of some of the WP functions, although lightweight responses are preferable 9 times out of 10.
So you could either get the value by calling get_term on each id (and using the $term->count in the response).
OR you could use wp_list_categories with your $secondary_terms as the include parameter.
The latter, obviously, does more of the work for you and it may be that, with a little CSS, it would be exactly what you want. The former is a little more work, but would give you very fine control over what get's sent out.
Performance-wise, wp_list_categories has the advantage of being just one database call. However it takes a fair amount of set-up (internally) so, with caching, there's every reason that a series of single calls will perform just as well, particularly if it's a short list.
Daniel Tomas comments:
Hi John, I was testing the code you provide...
In the second loop how can I get the terms only once??
For example if there are 2 canyoning activities (activities taxonomy) in Spain (place taxonomy) , now the term "Spain" is showed twice..
You can see the example in the image..
Thanks your your help.