Basically I have a custom post type of ‘products’ which has two taxonomies attached to it…the normal ‘category’ and a custom taxonomy called ‘brands’.
I have a page which is ‘brand’ specific. On this page I’d like to list all the ‘categories’ that have a ‘product’ in them with a term of the ‘brand’ whos page I’m on attached.
Eg. say I’m on the “Nike” page. I want it to list all categories that have a ‘product’ in them with the ‘brand’ of “Nike” attached to them.
My initial thoughts are to use get_categories but theres now way to define a specific taxonomy or ‘brand’?
$categories = get_categories('orderby=name&depth=1&hide_empty=0&child_of='.$cat);
Anyone done this before or knows a way to query the database directly to get the required results?
Any help is much appreicated, Thanks
Hi @daveaspi:
What you want to do is common but not well handled in WordPress core. There are probably ways to do it without custom SQL but I don’t think they would scale for a large number of posts. Below is a function I wrote called
get_cross_referenced_terms()
that will get what you want, complete with an example how to use it.This following code can be placed in the root of your WordPress site in a
test.php
file tosee it work. You can then copy the function
get_cross_referenced_terms()
into your theme’sfunctions.php
file or into a.php
file of a plugin you might be working on:If you want to list all the available categories for a custom post type, this snippet might help you.
Just use the standard
get_categories()
function and pass the$args
relating to the taxonomy you registered for your CPT. So, if you defined your taxonomy like this:Then you’d show the taxonomy to front end users via:
You will see an object which will help you on your way.
You can either code a custom sql query or you can query your post type for that “brand” and collect the categories and then display them, something like:
and here you have the list of all categories ID in an array $my_cats.
which you can get all the info you need from