我正在开发一个插件,它将被放置在我的woocommerce产品侧边栏中 . 我需要这个,给定产品ID /对象,它会找到2个产品,它具有我之前创建的相同的自定义分类 .
使用此代码,我获得了产品中使用的术语列表 "collane" is the custom taxonomy :
get_the_term_list( $product->id, 'collane', '<div style="direction:rtl;">', '</div>', '' );
问题是我不知道如何获取自定义分类标识,以及如何通过我的自定义分类法过滤它 .
我已经使用WP_Query使用以下代码查找同一类别的产品:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $atts['limit'],
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $cat_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
)
)
);
如何更改我的代码以获取所需的分类标识/对象,然后在我的WP_Query中使用它?
1 回答
您应该尝试以下
WP_Query
$args
,这将允许从同一个"collane"自定义分类中获得与当前产品具有相同术语的其他2个产品 .代码: