自定义分类和查询多个条件

时间:2016-07-07 作者:Bashar Gh

我有这个问题

$query->set( \'tax_query\', array(
                    \'relation\' => \'OR\',
                    array(
                        \'taxonomy\' => \'location\',
                         \'operator\' => \'IN\',
                        \'field\' => \'slug\',
                        \'terms\' => $availableRegions,
                    ),
                    array(
                        \'taxonomy\' => \'location\',
                         \'operator\' => \'NOT IN\',
                        \'field\' => \'slug\',
                        \'terms\' => $notavailableRegions,
                    )
                ));
返回这个sql查询

SELECT wp_posts.ID
FROM wp_posts 
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id) JOIN wp_icl_translations t
ON wp_posts.ID = t.element_id 
AND t.element_type IN (\'bla\') JOIN wp_icl_languages l
ON t.language_code=l.code
AND l.active=1
WHERE 1=1  


AND (
wp_term_relationships.term_taxonomy_id IN (200) 

OR wp_posts.ID NOT IN ( 
SELECT object_id 
FROM wp_term_relationships 
WHERE term_taxonomy_id IN (201,203,205,206) )
)
但我真正需要的是返回这个sql

SELECT wp_posts.ID
FROM wp_posts 
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id) JOIN wp_icl_translations t
ON wp_posts.ID = t.element_id 
AND t.element_type IN (\'bla\') JOIN wp_icl_languages l
ON t.language_code=l.code
AND l.active=1
WHERE 1=1  


AND (
wp_term_relationships.term_taxonomy_id IN (200) 
OR wp_term_relationships.term_taxonomy_id NOT IN (201,203,205,206) 

)
有什么建议可以挽救我的周末:D

1 个回复
SO网友:P-S

假设您有5个实例:200、201、202、203、204

NOT IN {200, 201, 202} 等于IN {203, 204} (所有其他实例)。

因此,您不再需要IN操作符,因为它的结果已经包含在使用NOT IN操作符时。

在您的情况下,假设一篇文章没有多个位置,这种逻辑就可以工作。

$query->set(\'tax_query\',

        array(
            array (
                \'taxonomy\' => \'location\',
                \'operator\' => \'NOT IN\',
                \'field\' => \'slug\',
                \'terms\' => $notavailableRegions,
            )
        )
);

相关推荐

I receive taxonomy id

您好,我想从字段中获取值,但我收到的id代码如下:function add_product_column( $columns ) { //add column $columns[\'new_column\'] = __( \'New column\', \'woocommerce\' ); return $columns; } add_filter( \'manage_edit-product_columns\', \'add_pr