我就差这么一点了,这一件事不管用了.感谢任何帮助,尽管我开始认为这可能是WordPress的一个限制?
这是为自定义帖子类型页面上的相关产品区域.这个网站上的帖子和产品都使用"模特"分类,每个帖子经常使用该税种中的几个术语.因此,当在带有特定术语的帖子页面上时,我们希望显示特定(默认为WooCommerce)类别的产品,这些类别也具有与当前帖子相同的所有‘Model’分类术语.
因此,我让下面的代码正常工作,只是它不会返回包含$TAX_Slugs数组中所有术语的产品.它将退回所有包含任何条款的产品.如果我在嵌套数组中包含‘and’运算符,它将不返回任何内容.
希望这是有道理的.谢谢!
1st Try个
add_action( 'elementor/query/model_loop_free', function( $query ) {
//get ID of current post
$id = get_queried_object_id();
// get current taxonomy - in this case the make/model
$taxos = get_the_terms( $id, 'models');
foreach ( $taxos as $tax ) {
$tax_slugs[] = $tax->slug;
}
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'models',
'field' => 'slug',
'terms' => $tax_slugs,
//'operator' => 'AND'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'free-play-adapter'
),
);
$query->set( 'tax_query', $tax_query );
} );
使用上面的代码,$TAX_SLUGS返回以下格式: 数组([0]=>;m100c[1]=>;Seeburg)
2nd Try我还try 了一种不同的方法,无论我是否使用运算符,都不会返回任何产品帖子:
add_action( 'elementor/query/model_loop_free', function( $query ) {
$id = get_queried_object_id();
$taxos = get_the_terms( $id, 'models');
// convert array of term objects to array of term slugs
$tax_array = array();
foreach ( $taxos as $tax ) {
array_push($tax_array,$tax->slug);
}
$tax_slugs = "'" . implode("','", $tax_array) . "'";
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'models',
'field' => 'slug',
'terms' => $tax_slugs,
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'free-play-adapter',
),
);
$query->set( 'tax_query', $tax_query );
} );
使用上面的代码,$TAX_SLUGS以以下格式返回: 《M100c》、《Seeburg》
3rd Try,我的最后一次伪代码try (可能是?)try 将每个术语创建为单独的嵌套数组,返回任何术语,但不是全部:
add_action( 'elementor/query/model_loop_free', function( $query ) {
$id = get_queried_object_id();
$taxos = get_the_terms( $id, 'models');
foreach ( $taxos as $tax ) {
$tax_slugs[] = array(
"'taxonomy' => 'models',
'field' => 'slug',
'terms' => '" .$tax->slug. "',
'operator' => 'AND'"
);
}
$tax_query = array(
'relation' => 'AND',
array($tax_slugs),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'free-play-adapter',
),
);
$query->set( 'tax_query', $tax_query );
} );
使用上面的代码,$TAX_SLUGS以以下格式返回: 数组([0]=>;‘soronomy’=>;‘Models’,‘field’=>;‘slug’,‘Terms’=>;‘m100c’)[1]=>;数组([0]=>;‘soronomy’=>;‘Models’,‘field’=>;‘slug’,‘Terms’=>;‘Seeburg’))