WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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,
  $sql = <<<SQL
  COUNT(*) AS post_count
  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
  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
  $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


share|improve this question
If anybody has any tips or help on this I'd be very grateful as its been haunting me for a good few days now! Think if I could use child_of => $parent_category it would work great? – daveaspinall Mar 14 '11 at 14:53
Any progress on this Q? – kaiser Apr 19 '12 at 3:13

You can get the parent category pretty simple: It's assigned to the object.

$cat = get_category( $q_cat );
// Get the ID
echo $cat->category_parent;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.