Load alternate taxonomy template based off of private query_var in wp_query term
-
Load alternate taxonomy template based off of private query_var in wp_query term links
I have multiple instances where wp_query is displaying a list of the same taxonomies term links(ALL terms). Each instance is unique because the tax query includes an AND relation with an additional taxonomy term.
Two separate queries both listed by the same term-names/term_links`$terms = get_terms( ‘term-001’, ‘orderby=name’ );
$i = 0;
foreach ($terms as $term) {
$args = array(
‘post_type’ => ‘post-type-001’,
‘posts_per_page’ => -1,
‘tax_query’ => array(
‘relation’ => ‘AND’,
array(
‘taxonomy’ => ‘tax-002’,
‘terms’ => ‘term-001’,
‘field’ => ‘slug’
),
array(
‘taxonomy’ => ‘tax-001’,
‘field’ => ‘slug’,
‘terms’ => $term->slug
)
)
);
$term_link = get_term_link( $term );
$new2 = new WP_Query($args);
if($new2->have_posts()){
echo “<li><a href='”.$term_link.”‘>”.$term->name.”</a></li>”;}
}
///////////////////
$terms = get_terms( ‘term-001’, ‘orderby=name’ );
$i = 0;
foreach ($terms as $term) {
$args = array(
‘post_type’ => ‘post-type-001’,
‘posts_per_page’ => -1,
‘tax_query’ => array(
‘relation’ => ‘AND’,
array(
‘taxonomy’ => ‘tax-002’,
‘terms’ => ‘term-002’,
‘field’ => ‘slug’
),
array(
‘taxonomy’ => ‘tax-001’,
‘field’ => ‘slug’,
‘terms’ => $term->slug
)
)
);
$term_link = get_term_link( $term );
$new2 = new WP_Query($args);
if($new2->have_posts()){
echo “<li><a href='”.$term_link.”‘>”.$term->name.”</a></li>”;}
} `
This works as intended. The problem is that the generated term links all go to the same archive template (taxonomy-tax-001). I can either modify that archive template(taxonomy-tax-001) to list posts with arguments that match my first query or my second query, but I cant wrap my head around how to have the term links from each query to link to archives that will properly display only posts that match the full criteria from the query they were linked to from.
I attempted to add a custom variable to the query where the terms are listed. ‘my_special_query’ => true,
`$terms = get_terms( ‘term-001’, ‘orderby=name’ );
$i = 0;
foreach ($terms as $term) {
$args = array(
‘post_type’ => ‘post-type-001’,
‘posts_per_page’ => -1,
‘my_special_query’ => true,
‘tax_query’ => array(
‘relation’ => ‘AND’,
array(
‘taxonomy’ => ‘tax-002’,
‘terms’ => ‘term-002’,
‘field’ => ‘slug’
),
array(
‘taxonomy’ => ‘tax-001’,
‘field’ => ‘slug’,
‘terms’ => $term->slug
)
)
);
$term_link = get_term_link( $term );
$new2 = new WP_Query($args);
if($new2->have_posts()){
echo “<li><a href='”.$term_link.”‘>”.$term->name.”</a></li>”;}
}`
Then attempted to attach a filter based of the custom query_var in my functions.php file:
`function new_query_vars( $query_vars ){
$query_vars[] = ‘my_special_query’;
return $query_vars;
}
add_filter( ‘query_vars’, ‘new_query_vars’ );add_filter(‘template_include’, ‘alt_template’);
function alt_template( $template ){
$terms = get_terms( ‘tax-001’);
$new = get_query_var(‘my_special_query’);
global $wp_query;
foreach ($terms as $term) {
$term_link = get_term_link( $term );
if( isset($wp_query->query_vars[‘my_special_query’])) {
return locate_template(‘new-taxpage.php’);
}
return $template;
}}`
This does not affect what template the term links are opening in as intended.
- The topic ‘Load alternate taxonomy template based off of private query_var in wp_query term’ is closed to new replies.