所以基本上我使用以下代码似乎完全把所有内容都放入查询中,但是,我仍然得到错误的产品 . 例如,我要求的产品类别为“冬季”,但仍然会收到“全季”类别的产品

这是我的代码:

add_action('pre_get_posts', 'advanced_search_query', 1000);
function advanced_search_query($query) {
if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
    global $wp_query;
    // category terms search
    $categories = array('winter','all-season','summer','run-flat');
    $category = $_GET['category'];
    $categories = array_diff($categories,array($category));
    $taxonomy = 'product_cat';
    if (isset($category) && !empty($category)) {
        $args = array(
            'relation' => 'AND',
            array(
                'taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => $category,
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => $taxonomy,
                'field' => 'slug',
                'terms' => array($categories),
                'operator' => 'NOT IN'
            )
        );
        $query->set('tax_query', $args);
        print_r($query);
    }
    return $query;
    }
}

print_r返回以下内容:

WP_Query Object([query] => Array([s] => 2457516 [post_type] => product)[query_vars] =>数组

[s] => 2457516
[post_type] =>产品
[错误] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[静态] =>
[pagename] =>
[page_id] => 0
[second] =>
[分钟] =>
[小时] =>
[day] => 0
[monthnum] => 0
[年] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[作者] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] => total_sales
[meta_value] =>
[预览] =>
[句子] =>
[fields] =>
[menu_order] =>
[category__in] =>数组

[category__not_in] =>数组

[category__and] =>数组

[post__in] =>数组

[post__not_in] =>数组

[tag _in] =>数组

[tag__not_in] =>数组

[tag__and] =>数组

[tag_slug__in] =>数组

[tag_slug__and] =>数组

[post_parent__in] =>数组

[post_parent__not_in] =>数组

[author__in] =>数组

[author__not_in] =>数组

[orderby] => menu_order Headers
[order] => ASC
[meta_query] =>数组

[0] =>数组

[key] => _visibility
[value] =>数组

[0] =>可见
[1] =>搜索

[比较] => IN

[1] =>数组

[key] => _stock_status
[value] => instock
[比较] => =

[posts_per_page] => 12
[wc_query] => product_query
[tax_query] =>数组

[关系] => AND
[0] =>数组

[taxonomy] => product_cat
[field] => slug
[条款] =>冬天
[operator] => IN

[1] =>数组

[taxonomy] => product_cat
[field] => slug
[terms] =>数组

[1] =>全季节
[2] =>夏天
[3] =>逃避

[operator] => NOT IN

[tax_query] => WP_Tax_Query对象

[queries] =>数组

[关系] => AND
[table_aliases:protected] =>数组

[queried_terms] =>数组

[primary_table] =>
[primary_id_column] =>

[meta_query] =>
[date_query] =>
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] => 1
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] => 1
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] =>
[is_404] =>
[is_comments_popup] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] => 1
[query_vars_hash:WP_Query:private] => ac61ad4fe3856f3f91e2ca85a7e667a1
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[停用词:WP_Query:private] =>
[compat_fields:WP_Query:private] =>数组

[0] => query_vars_hash
[1] => query_vars_changed

[compat_methods:WP_Query:private] =>数组

[0] => init_query_flags
[1] => parse_tax_query

SQL查询:

SELECT SQL_CALC_FOUND_ROWS itq2_posts.ID FROM itq2_posts INNER JOIN itq2_term_relationships ON(itq2_posts.ID = itq2_term_relationships.object_id)INNER JOIN itq2_postmeta ON(itq2_posts.ID = itq2_postmeta.post_id)INNER JOIN itq2_postmeta AS mt1 ON(itq2_posts.ID = mt1.post_id)INNER JOID itq2_postmeta AS mt2 ON(itq2_posts.ID = mt2.post_id)WHERE 1 = 1 AND(itq2_term_relationships.term_taxonomy_id IN(300)AND itq2_posts.ID NOT IN(SELECT object_id FROM itq2_term_relationships WHERE term_taxonomy_id IN(8,1901,1902,1905, 1906,1907,1908,1909,1910,1911,1912,1913,1914,1915,1917,1919,1920,1921,1922,1923,1924,1925,1926,1927,1928,1929,1930,1931,1932, 1933,1934,1935,1936,1937,1938,1939,1940,1941,1943,1945,1946,1947,1948,1949,1950,1951,1952,1953,1954,1955,1957,1959,1960,1962, 1965,1968,1971,1974,1976,1979,1981,1982,1983,1985,1988,1990,1991,1992,1995,1997,1999,2002,2004,2005,2007,2010,2012,2014,2017, 2020,2023,2769,2770,2776,2780,3421,3479,3529,3540,3543,3549,3555,3592,3600,3632,3725,3729 ,3734,3738,3775,3789,3882,3890)))和(((itq2_posts.post_title LIKE'%2457516%')或(post_excerpt LIKE'%2457516%')或(itq2_posts.post_content LIKE'%2457516%' )))和(itq2_postmeta.meta_key='total_sales'AND((mt1.meta_key ='_ visible“AND CAST(mt1.meta_value AS CHAR)IN('visible','search'))AND(mt2.meta_key ='_ stock_status'AND CAST(mt2.meta_value AS) CHAR)='instock')))和itq2_posts.post_type ='product'AND(itq2_posts.post_status ='publish'或itq2_posts.post_status ='private')GROUP BY itq2_posts.ID ORDER BY itq2_postmeta.meta_value 0 DESC,itq2_posts . post_date DESC LIMIT 0,12