Last active
October 17, 2016 16:44
-
-
Save stephanieleary/4e5a2d5d65a655d522ec20d356782a96 to your computer and use it in GitHub Desktop.
add post_type arg to term links (narrow down the query)
This file contains hidden or 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
<?php | |
// output function extracted from a custom term list widget | |
function scl_list_terms( $taxonomy ) { | |
$labels = get_taxonomy_labels( $taxonomy ); | |
$tax_args = array( | |
'taxonomy' => $taxonomy, | |
'title_li' => '', | |
'show_option_none' => $labels->not_found, | |
); | |
$tax_args = apply_filters( 'widget_tax_menu_args', $tax_args ); | |
add_filter( 'term_link', 'scl_taxonomy_link_for_post_type', 10, 3 ); | |
printf( '<ul class="tax-term-list">%s</ul>', wp_list_categories( $tax_args ) ); | |
remove_filter( 'term_link', 'scl_taxonomy_link_for_post_type', 10 ); | |
} | |
function scl_taxonomy_link_for_post_type( $termlink, $term, $taxonomy ) { | |
$tax_obj = get_taxonomy( $taxonomy ); | |
// if this is not a shared taxonomy, bail | |
if ( count( $tax_obj->object_type ) == 1 ) | |
return $termlink; | |
// cf. https://gist.github.com/sillybean/c725ea4620e9d434e71927ab41879f38 | |
$post_type = scl_find_post_type(); | |
if ( !isset( $post_type ) || 'any' == $post_type ) | |
return $termlink; | |
$termlink = add_query_arg( 'post_type', $post_type, $termlink ); | |
return $termlink; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment