2

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

2
  • 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? Mar 14, 2011 at 14:53
  • Any progress on this Q?
    – kaiser
    Apr 19, 2012 at 3:13

1 Answer 1

3

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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