我有一个定制的post类型产品(葡萄酒)。以下各项都是自定义分类法:
在葡萄酒的单一产品视图中,我想进行以下查询:
1) 查找所有具有same family, same size 但是来自different vintage
2) 查找same family 并拥有same vintage 还有一个different size.
现在的问题是numeric year 是的自定义术语元属性vintage term. 这个numeric bottle size 是大小术语的自定义术语元属性。
对于第一个查询,我尝试了以下操作:
$other_years = get_posts(array(
\'post_type\' => \'product\',
\'numberposts\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'family\',
\'field\' => \'id\',
\'terms\' => $family->term_id, // Where term_id of Term 1 is "1".
\'include_children\' => false
),
array(
\'taxonomy\' => \'size\',
\'field\' => \'id\',
\'terms\' => $size->term_id, // Where term_id of Term 1 is "1".
\'include_children\' => false
),
array(
\'taxonomy\' => \'vintage\',
\'field\' => \'id\',
\'terms\' => array($vintage->term_id), // Where term_id of Term 1 is "1".
\'include_children\' => false,
\'operator\' => \'NOT IN\'
),
)
));
It works. 然而:与其比较
term_id (此处:年份的术语id)我想比较术语自定义元值(year\\u numeric)是否不同。
有没有可能进行这样的查询?
最合适的回答,由SO网友:jgangso 整理而成
AFAIK在单个WP\\u查询中无法实现这一点,因此您必须首先获得一个具有different year 而不是问题中的那个。
我认为通过以下几点,你会非常接近。(没有要立即测试的env)
$other_years_terms = get_terms(
\'taxonomy\' => \'vintage\',
\'meta_key\' => \'year_numeric\',
\'meta_value\' => $the_current_wine_year, // You\'ll have to figure that out
\'meta_compare\' => \'!=\',
\'fields\' => \'ids\'
);
$other_years_posts = get_posts(array(
\'post_type\' => \'product\',
\'numberposts\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'family\',
\'field\' => \'id\',
\'terms\' => $family->term_id, // Where term_id of Term 1 is "1".
\'include_children\' => false
),
array(
\'taxonomy\' => \'size\',
\'field\' => \'id\',
\'terms\' => $size->term_id, // Where term_id of Term 1 is "1".
\'include_children\' => false
),
array(
\'taxonomy\' => \'vintage\',
\'field\' => \'term_id\',
\'terms\' => $other_years_terms,
),
)
));