我就差这么一点了,这一件事不管用了.感谢任何帮助,尽管我开始认为这可能是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’))

推荐答案

您的问题是需要使用AND关系,但要让它按您所希望的方式工作,您需要在TAX_QUERY中添加几个条件,而不是一个包含所有标记的条件. 另外,您有两个不同的条件:一个用于product_cat,另一个用于一个(或多个"型号").

您可以将所有参数设置为同一级别或子查询,因为TAX_QUERY接受嵌套条件(就像SQL中的子查询).

为了将相关条件组合在一起,我在这里使用子查询.

这里有一个伪代码来更直观地解释它:

// tax_query
$tax_query = [
    'relation'  =>  'AND', // this relation is between the product_cat and the models
    [], // condition relative to the product_cat
    // the sub query containing all models
    [
        'relation'  =>  'AND', // this relation is for the models between each other
        [], // condition one (ex: model 1)
        [], // condition two (ex: model 2)
        [], // condition three (ex: model 3)
        //...   
    ] 
];

因此,您的代码将如下所示:

add_action( 'elementor/query/model_loop_free', function ( $query ) {
    // get current product id
    $product_id = get_queried_object_id();

    //get current product's models
    $taxos = get_the_terms( $product_id, 'models'); 
    
    // Initial tax query (limit to the specific product_cat)
    $tax_query = [
        'relation' => 'AND',
        [
            'taxonomy'  => 'product_cat',
            'field'     => 'slug',
            'terms'     => 'free-play-adapter'
        ]
    ];
    
    // Generate sub tax parameters (all models)
    $sub_tax_query = [
        'relation'  =>  'AND'
    ];
    // Add one condition per "model"
    foreach ( $taxos as $tax ) {
        $sub_tax_query[] = [
            'taxonomy'  =>  'models',
            'field'     =>  'slug',
            'terms'     =>  $tax->slug
        ];
    }

    // Inject sub query (models) as the second condition to the main tax_query
    $tax_query[] = $sub_tax_query;

    // Add finnaly inject tax_query to query
    $query->set( 'tax_query', $tax_query );
} );

Php相关问答推荐

在不刷新页面的情况下无法使用JQuery更新id

如果WooCommerce中存在特定的运输方式,则隐藏其他运输方式

PHP日期操作,将多个日期输出为格式化字符串

允许在WooCommerce管理员优惠券列表中显示自定义优惠券类型

删除了数组中的值,而不是多个数组

如何使用MS Graph PHP SDK V2下载文件

Laravel区分大小写搜索

允许客户定义特定WooCommerce产品的价格

Laravel Carbon DiffForHumans选项(年,月,天)

PHP SimpleXML:按属性值访问 node

从WooCommerce管理订单汇总中隐藏原始小计行

如何解决 Laravel 外键错误?

关于Laravel缓存出现的错误

将 PHP 版本更改为 8.1,但 WordPress 认为我们正在使用版本 7.4?

无法在 Laravel 中实例化抽象类 ProductAbstract

如何正确判断 PHP 是否已正确配置为使用 DOMDocument?

Shopware 6 AuthControllerDecorator已弃用

Composer自动加载器重复了子类的类文件路径

如何通过工厂为点列制作一个假坐标?

基于其他数组创建多维数组 struct