I have a custom taxonomy page, it also has a slider up top. The problem is the slider displays random posts from all taxonomies rather than the current taxonomy. Is there a way to filter the images in the slider so it would only use the current taxonomy?
<?php
global $taxonomy_location_url, $taxonomy_profile_url;
$args = array(
'post_type' => $taxonomy_profile_url,
'orderby' => 'rand',
'meta_query' => array( array('key' => 'featured', 'value' => '1', 'compare' => '=', 'type' => 'NUMERIC') ),
'posts_per_page' => get_option("slideritems")
);
$q = query_posts($args);
if ( have_posts()) :
while ( have_posts() ) : the_post();
$category = wp_get_post_terms(get_the_ID(), $taxonomy_location_url);
$linktitle = get_the_title();
$imagealt = get_the_title();
?>
I have a custom taxonomy page, it also has a slider up top. The problem is the slider displays random posts from all taxonomies rather than the current taxonomy. Is there a way to filter the images in the slider so it would only use the current taxonomy?
<?php
global $taxonomy_location_url, $taxonomy_profile_url;
$args = array(
'post_type' => $taxonomy_profile_url,
'orderby' => 'rand',
'meta_query' => array( array('key' => 'featured', 'value' => '1', 'compare' => '=', 'type' => 'NUMERIC') ),
'posts_per_page' => get_option("slideritems")
);
$q = query_posts($args);
if ( have_posts()) :
while ( have_posts() ) : the_post();
$category = wp_get_post_terms(get_the_ID(), $taxonomy_location_url);
$linktitle = get_the_title();
$imagealt = get_the_title();
?>
Share
Improve this question
edited Apr 5, 2019 at 7:10
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Apr 4, 2019 at 18:55
Joe LandryJoe Landry
277 bronze badges
1
- What is the name of that custom taxonomy? – Krzysiek Dróżdż Commented Apr 5, 2019 at 6:59
2 Answers
Reset to default 1You’ve posted just the part of code that is responsible for getting the slider, I guess, so it’s a little bit hard to know, where exactly do you use it. But I assume that you use template hierarchy correctly and you use category archives.
If so, then you can get current term ID with get_queried_object_id()
and use it in your query.
So here’s the code that should solve your problem:
<?php
global $taxonomy_location_url, $taxonomy_profile_url;
$args = array(
'post_type' => $taxonomy_profile_url,
'orderby' => 'rand',
'meta_query' => array( array('key' => 'featured', 'value' => '1', 'compare' => '=', 'type' => 'NUMERIC') ),
'posts_per_page' => get_option("slideritems"),
'tax_query' => array( array( 'taxonomy' => '<YOUR TAXONOMY NAME>', 'terms' => get_queried_object_id() ) ),
);
$q = new WP_Query($args);
if ( $q->have_posts()) :
while ( $q->have_posts() ) : $q->the_post();
$category = wp_get_post_terms(get_the_ID(), $taxonomy_location_url);
$linktitle = get_the_title();
$imagealt = get_the_title();
...
endwhile;
wp_reset_postdata();
?>
All you have to do is to put your taxonomy name in there.
Also please notice, that I’ve changed query_posts
to new WP_Query
- it’s much nicer way to perform your custom queries, because you don’t overwrite the global query.
Use this code and modify the texonomy name .hopefully it might work.
<?php
global $taxonomy_location_url, $taxonomy_profile_url;
$terms = wp_get_post_terms( $post->ID, 'taxonomy name'); // to get my taxonomy
$args = array(
'post_type' => $taxonomy_profile_url,
'orderby' => 'ID',
'meta_query' => array( array(
'key' => 'featured',
'value' => '1',
'compare' => '=',
'type' => 'NUMERIC'
) ),
'posts_per_page' => get_option("slideritems"),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'texonomy_name',
'field' => 'ID',
'terms' => $terms
)
),
);
$q = query_posts($args);
if ( have_posts()) :
while ( have_posts() ) : the_post();
$category = wp_get_post_terms(get_the_ID(), $taxonomy_location_url);
$linktitle = get_the_title();
$imagealt = get_the_title();
?>