我有一个产品属性,叫做;“颜色”;当我在归档页面(例如,color=blue)上时,我想显示包含该属性为color=blue的产品的所有父产品类别的列表。
我尝试使用get\\u queryed\\u object\\u id()和get\\u terms()来获取归档术语(蓝色),但我无法理解。我想重申,我想检索术语列表,而不是帖子。
如果有人能把我引向正确的方向,我将不胜感激!
This question 看起来与我想要的类似,但是它使用wpdb,并且希望使用常规查询。
我曾尝试过这样做,以获得分类法(pa\\U颜色和产品类别)中所有帖子的列表,但我不确定如何获得仅包含类别的列表。
$current_color = get_queried_object_id();
$query = new WP_Query( array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'pa_color\',
\'field\' => \'term_id\',
\'terms\' => $current_color,
\'operator\' => \'AND\'
),
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'term_id\',
\'terms\' => $cats
)
)
) );
最合适的回答,由SO网友:ViralP 整理而成
尝试替换\'operator\' => \'AND\'
具有\'relation\'=>\'AND\'
更新的代码段:
$current_color = get_queried_object_id();
$query = new WP_Query( array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'tax_query\' => array(
\'relation\' => \'AND\'
array(
\'taxonomy\' => \'pa_color\',
\'field\' => \'term_id\',
\'terms\' => $current_color,
),
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'term_id\',
\'terms\' => $cats
)
)
) );
参考号:
https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters ()
Multiple Taxonomy Handling )
编辑1:您可以编辑以上查询以仅返回ids
(“fields”=>;“ids”)并使用get_terms
让这些ID获取类别列表
get_terms( array(
\'taxonomy\' => \'product_cat\',
\'object_ids\' => $posts_matching_criteria
);
参考文献1:
https://developer.wordpress.org/reference/functions/get_terms/参考文献2:https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#user-contributed-notes