Thanks to @MikeSchinkel I have the following code:
$term = get_query_var('term');
$brand = get_term_by('slug',$term,'brands'); // This here just to illustrate
$categories = get_cross_referenced_terms(array(
'post_type' => 'Products',
'taxonomy' => 'category',
'related_taxonomy' => 'brands',
'term_id' => $brand->term_id
));
which uses the following function:
// query to get categories for a specific tag
function get_cross_referenced_terms($args) {
global $wpdb;
$args = wp_parse_args($args,array(
'post_type' => 'Products',
'taxonomy' => 'category',
'related_taxonomy' => 'brands',
'term_id' => 0,
));
extract($args);
$sql = <<<SQL
SELECT DISTINCT
{$wpdb->terms}.*,
COUNT(*) AS post_count
FROM
{$wpdb->terms}
INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id={$wpdb->term_taxonomy}.term_id
INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_taxonomy}.term_taxonomy_id={$wpdb->term_relationships}.term_taxonomy_id
INNER JOIN {$wpdb->posts} ON {$wpdb->term_relationships}.object_id={$wpdb->posts}.ID
INNER JOIN {$wpdb->term_relationships} related_relationship ON {$wpdb->posts}.ID=related_relationship.object_id
INNER JOIN {$wpdb->term_taxonomy} related_term_taxonomy ON related_relationship.term_taxonomy_id=related_term_taxonomy.term_taxonomy_id
INNER JOIN {$wpdb->terms} related_terms ON related_term_taxonomy.term_id=related_terms.term_id
WHERE 1=1
AND related_terms.term_id<>{$wpdb->terms}.term_id
AND {$wpdb->posts}.post_type='%s'
AND {$wpdb->term_taxonomy}.taxonomy='%s'
AND related_term_taxonomy.taxonomy='%s'
AND related_terms.term_id=%d
AND {$wpdb->term_taxonomy}.parent=0
GROUP BY
{$wpdb->terms}.term_id
SQL;
$sql = $wpdb->prepare($sql,$post_type,$taxonomy,$related_taxonomy,$term_id);
$terms = $wpdb->get_results($sql);
return $terms;
}
This is used on the category.php page and works great, but I need to now use the same code (ideally) on the child category page?
So for example I have it on a category page of 'trainers', filtered by brand of 'nike' for the custom post type 'products'.
I now need it to show all child categories of 'trainers', also filtered by brand of 'nike' for custom post type of 'products'.
Is there a way to simply add?
'category_id' => '$parent_category',
Any help would be greatly appreciated! Thanks
Dave
child_of => $parent_category
it would work great?