wp query - Filter Custom Taxonomy Posts

I have a custom taxonomy page, it also has a slider up top. The problem is the slider displays random posts from all tax

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
Add a comment  | 

2 Answers 2

Reset to default 1

You’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();

?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745623354a4636655.html

相关推荐

  • wp query - Filter Custom Taxonomy Posts

    I have a custom taxonomy page, it also has a slider up top. The problem is the slider displays random posts from all tax

    11小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信