admin管理员组文章数量:1025465
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();
?>
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();
?>
本文标签: wp queryFilter Custom Taxonomy Posts
版权声明:本文标题:wp query - Filter Custom Taxonomy Posts 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745623352a2159710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论