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'
)),
);