Created
September 27, 2012 16:34
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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