admin管理员组文章数量:1291676
I have a product attribute called "color" and when I am on the archive page (e.g. color=blue) I want to display a list of all the parent product categories that contain products with that attribute, color=blue.
I tried using get_queried_object_id() to get the archive term (blue), in conjunction with get_terms(), however I couldn't figure it out. I'd like to reiterate that I want to retrieve the list of terms and not the posts.
If anyone can lead me in the right direction, I'd appreciate it!
This question seems similar to what I want, however it uses wpdb, and want to use a regular query.
I was tried doing something like this, to get a list of all posts that are in both taxonomies (pa_color and product category), but I am not sure how to get a list of just categories.
$current_color = get_queried_object_id();
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'pa_color',
'field' => 'term_id',
'terms' => $current_color,
'operator' => 'AND'
),
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cats
)
)
) );
I have a product attribute called "color" and when I am on the archive page (e.g. color=blue) I want to display a list of all the parent product categories that contain products with that attribute, color=blue.
I tried using get_queried_object_id() to get the archive term (blue), in conjunction with get_terms(), however I couldn't figure it out. I'd like to reiterate that I want to retrieve the list of terms and not the posts.
If anyone can lead me in the right direction, I'd appreciate it!
This question seems similar to what I want, however it uses wpdb, and want to use a regular query.
I was tried doing something like this, to get a list of all posts that are in both taxonomies (pa_color and product category), but I am not sure how to get a list of just categories.
$current_color = get_queried_object_id();
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'pa_color',
'field' => 'term_id',
'terms' => $current_color,
'operator' => 'AND'
),
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cats
)
)
) );
Share
Improve this question
edited May 24, 2021 at 18:39
Edegist
asked May 24, 2021 at 3:51
EdegistEdegist
1331 silver badge10 bronze badges
2
- Paste a code that you used – anton Commented May 24, 2021 at 10:15
- @anton Iadded some code to the post, however I couldn't get anything to work properly. – Edegist Commented May 24, 2021 at 18:40
1 Answer
Reset to default 4Try replacing 'operator' => 'AND'
with 'relation'=>'AND'
Updated Code Snippet:
$current_color = get_queried_object_id();
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND'
array(
'taxonomy' => 'pa_color',
'field' => 'term_id',
'terms' => $current_color,
),
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cats
)
)
) );
Reference : https://developer.wordpress/reference/classes/wp_query/#taxonomy-parameters ( Multiple Taxonomy Handling )
Edit 1: You can edit above query to return only ids
( 'fields' => 'ids' ) and use get_terms
for those IDs to get the list of categories
get_terms( array(
'taxonomy' => 'product_cat',
'object_ids' => $posts_matching_criteria
);
Ref 1 : https://developer.wordpress/reference/functions/get_terms/
Ref 2 : https://developer.wordpress/reference/classes/wp_term_query/__construct/#user-contributed-notes
本文标签: wp queryGet list of terms that have posts in another term
版权声明:本文标题:wp query - Get list of terms that have posts in another term 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741536801a2384073.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论