Skip to content

Instantly share code, notes, and snippets.

@jazbek
Created September 27, 2012 16:34
Show Gist options
  • Save jazbek/3794987 to your computer and use it in GitHub Desktop.
Save jazbek/3794987 to your computer and use it in GitHub Desktop.
Allow 'post_type' passed as an arg to get_terms (useful for taxonomies shared between post types)
/**
* terms_clauses
*
* filter the terms clauses in get_terms(), allow post_type arg
*
* @param $clauses array
* @param $taxonomy string
* @param $args array
* @return array
* @author Jessica Yazbek
**/
function terms_clauses($clauses, $taxonomy, $args)
{
global $wpdb;
// if post_type set, don't return terms that don't relate to the current post type
$post_type = $args['post_type'] ? $args['post_type'] : false;
if ($post_type)
{
$clauses['join'] .= " LEFT JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id";
$clauses['where'] .= " AND p.post_type='$post_type'";
$clauses['where'] .= " GROUP BY t.term_id";
}
return $clauses;
}
add_filter('terms_clauses', 'terms_clauses', 1000, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment