我有成功返回价格范围内产品的代码。然而,我还需要按一个名为“size”的产品属性进行过滤。
因此,我应该在给定的价格范围内获得一定尺寸的所有产品。
我的代码适用于价格范围部分,但我无法找到添加尺寸部分的正确方法。
这是我的代码:
$params = array(
\'posts_per_page\' => 20,
\'post_type\' => array(\'product\', \'product_variation\'),
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
array(
\'key\' => \'_price\',
\'value\' => $_POST[\'product_price_min\'],
\'compare\' => \'>=\',
\'type\' => \'NUMERIC\'
),
array(
\'key\' => \'_price\',
\'value\' => $_POST[\'product_price_max\'],
\'compare\' => \'<=\',
\'type\' => \'NUMERIC\'
)
),
array(
array(
\'taxonomy\' => \'pa_size\',
\'field\' => \'slug\',
\'terms\' => $_POST[\'size\'],
\'operator\' => \'IN\',
)
)
)
);
$products = new WP_Query($params);
最合适的回答,由SO网友:mrben522 整理而成
你把你的meta query 和tax query. 尝试以下操作:
$params = array(
\'posts_per_page\' => 20,
\'post_type\' => array(\'product\', \'product_variation\'),
\'meta_query\' => array(
array(
\'key\' => \'_price\',
\'value\' => $_POST[\'product_price_min\'],
\'compare\' => \'>=\',
\'type\' => \'NUMERIC\'
),
array(
\'key\' => \'_price\',
\'value\' => $_POST[\'product_price_max\'],
\'compare\' => \'<=\',
\'type\' => \'NUMERIC\'
),
),
\'tax_query\' => array(
array(
\'taxonomy\' => \'pa_size\',
\'field\' => \'slug\',
\'terms\' => $_POST[\'size\'],
\'operator\' => \'IN\',
)
)
);
$products = new WP_Query($params);