admin管理员组文章数量:1320591
I have created a loop that displays products, however I have ran into a problem when trying to filter them. I have added in tax_query because that is how to filter the search with taxonomies(based on my understanding). I have obtained the current urls term to filter with $term_search = get_queried_object()->slug;
and I have echo'd out $term_search to make sure it was outputing the correct information.
How do I get my filter to work properly? One thing to mention, I have changed my permalink for the categories, does that effect my slug?
$term_search = get_queried_object()->slug;
// WP_Query arguments
$args = array(
'p' => 'product',
'post_type' => array( 'product' ),
'order' => 'ASC',
'post_per_page' => 20,
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $term_search, // (the name of what you want to filter by (latest or whatever))
'include_children' => true,
'operator' => 'IN'
),
);
I have created a loop that displays products, however I have ran into a problem when trying to filter them. I have added in tax_query because that is how to filter the search with taxonomies(based on my understanding). I have obtained the current urls term to filter with $term_search = get_queried_object()->slug;
and I have echo'd out $term_search to make sure it was outputing the correct information.
How do I get my filter to work properly? One thing to mention, I have changed my permalink for the categories, does that effect my slug?
$term_search = get_queried_object()->slug;
// WP_Query arguments
$args = array(
'p' => 'product',
'post_type' => array( 'product' ),
'order' => 'ASC',
'post_per_page' => 20,
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $term_search, // (the name of what you want to filter by (latest or whatever))
'include_children' => true,
'operator' => 'IN'
),
);
Share
Improve this question
edited Oct 6, 2020 at 18:38
davidb3rn
asked Oct 6, 2020 at 17:55
davidb3rndavidb3rn
72 silver badges4 bronze badges
1 Answer
Reset to default 0I was missing the condition for the array inside the tax_query array. It is important to have it regardless if you have one or more conditions.
$term_search = get_queried_object()->slug;
// WP_Query arguments
$args = array(
'p' => 'product',
'post_type' => array( 'product' ),
'order' => 'ASC',
'post_per_page' => 20,
'tax_query' => array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $term_search, // (the name of what you want to filter by (latest or whatever))
'include_children' => true,
'operator' => 'IN'
)),
);
本文标签: plugin developmentHow do I add filter with woocommerce categories
版权声明:本文标题:plugin development - How do I add filter with woocommerce categories? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742078004a2419521.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论