Answer the question
In order to leave comments, you need to log in
How to get products with given woocommerce attribute?
Hey! I finish the filter of goods by attributes. After selecting the first attribute in the form, you need to get a list of products with this attribute. But the query returns an empty result when I add tax_query . The code is in functions.php
$newquery = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => '22',
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'pa_chrisi', // Product attribute taxonomy: always start with 'pa_'
'field' => 'term_id', // Can be 'term_id', 'slug' or 'name'
'terms' => 170,
), ),
) );
SELECT SQL_CALC_FOUND_ROWS dmg0j_posts.ID FROM dmg0j_posts WHERE 1=1 AND ( 0 = 1 ) AND dmg0j_posts.post_type = 'product' AND ((dmg0j_posts.post_status = 'publish')) GROUP BY dmg0j_posts.ID ORDER BY dmg0j_posts.post_date DESC LIMIT 0, 22
Answer the question
In order to leave comments, you need to log in
I found the answer myself.
If you are using WP_Query with tax_query in functions.php put your code in add_action with low priority:
add_action( 'init', 'maratgenius', 999 );
function maratgenius()
{
$newquery = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => '-1',
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'pa_chrisi', // Product attribute taxonomy: always start with 'pa_'
'field' => 'term_id', // Can be 'term_id', 'slug' or 'name'
'terms' => $_POST['get_what'],
), ),
) );
//other code
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question