Skip to content

Instantly share code, notes, and snippets.

@stephanieleary
Last active October 17, 2016 14:50
Show Gist options
  • Save stephanieleary/6b393626053e9a5986a663159855dbb7 to your computer and use it in GitHub Desktop.
Save stephanieleary/6b393626053e9a5986a663159855dbb7 to your computer and use it in GitHub Desktop.
add post_type arg to term links (narrow down the query)
<?php
add_filter( 'term_link', 'taxonomy_link_for_post_type', 10, 3 );
function 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