简体   繁体   English

通过WooCommerce上的WP-Query显示不正确的帖子

[英]Incorrect posts showing through WP-Query on WooCommerce

So basically I am using the following code which seems to be putting everything into the query perfectly however, I am still getting the wrong products. 因此,基本上我正在使用以下代码,似乎可以将所有内容完美地放入查询中,但是,我仍然得到错误的产品。 For example, I am requesting products with product category 'winter' but still receiving products in category 'all-season' 例如,我请求的产品类别为“冬季”,但仍收到“全季节”类别的产品

Here is my code: 这是我的代码:

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;
    }
}

The print_r returns the following: print_r返回以下内容:

WP_Query Object ( [query] => Array ( [s] => 2457516 [post_type] => product ) [query_vars] => Array ( [s] => 2457516 [post_type] => product [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [comments_popup] => [meta_key] => total_sales [meta_value] => [preview] => [sentence] => [fields] => [menu_order] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( WP_Query对象([查询] =>数组([s] => 2457516 [post_type] =>产品)[query_vars] =>数组([s] => 2457516 [post_type] =>产品[错误] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [ page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [标签] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [comments_popup] => [meta_key] => total_sales [meta_value] => [preview] => [sentence] => [fields] => [menu_order] => [category__in] => Array()[category__not_in] => Array()[category__and] => Array() [post__in] => Array()[post__not_in] => Array()[tag__in] => Array()[tag__not_in] => Array()[tag__and] => Array()[tag_slug__in] => Array()[tag_slug__and ] => Array()[post_parent__in] => Array()[post_parent__not_in] => Array()[author__in] => Array()[author__not_in] => Array( ) [orderby] => menu_order title [order] => ASC [meta_query] => Array ( [0] => Array ( [key] => _visibility [value] => Array ( [0] => visible [1] => search ) [compare] => IN ) [1] => Array ( [key] => _stock_status [value] => instock [compare] => = ) ) [posts_per_page] => 12 [wc_query] => product_query [tax_query] => Array ( [relation] => AND [0] => Array ( [taxonomy] => product_cat [field] => slug [terms] => winter [operator] => IN ) [1] => Array ( [taxonomy] => product_cat [field] => slug [terms] => Array ( [1] => all-season [2] => summer [3] => run-flat ) [operator] => NOT IN ) ) ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( ) [relation] => AND [table_aliases:protected] => Array ( ) [queried_terms] => Array ( ) [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 )[orderby] =>菜单标题[order] => ASC [meta_query] =>数组([0] =>数组([key] => _visibility [value] => Array([0] =>可见[1] =>搜索)[比较] => IN)[1] =>数组([键] => _stock_status [value] =>库存[compare] => =)))[posts_per_page] => 12 [wc_query] => product_query [tax_query] =>数组([关系] => AND [0] =>数组([分类法] => product_cat [field] =>子弹[terms] => winter [operator] => IN)[1] =>数组([分类法] => product_cat [字段] =>子弹[条款] =>数组([1] =>整个季节[2] =>夏季[3] =>放空)[运算符] =>不IN)))[tax_query] => WP_Tax_Query对象([查询] => Array()[关系] => AND [table_aliases:protected] => Array()[queried_terms] => Array()[primary_table] => [ primary_id_column] =>)[元查询] => [日期查询] => [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] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] => parse_tax_query ) ) _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] => ac61ad4fe3856f3f91e2ca85a7e667a > [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))

My guess is you have a double array for terms in your tax_query. 我的猜测是您的tax_query中的术语有双重数组。 You have: 你有:

$categories = array('...');

$args = array(
    //....
    array(
        'terms' => array( $categories )
    )
    //....
);

This should just be: 这应该是:

'terms' => $categories

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM