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'
),
);
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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'
)),
);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0