最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - How do I add filter with woocommerce categories?

programmeradmin1浏览0评论

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

1 Answer 1

Reset to default 0

I 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'
                        )),
                );
发布评论

评论列表(0)

  1. 暂无评论