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

时间:2015-11-12 21:32:44

标签: php wordpress search woocommerce wp-query

所以基本上我使用下面的代码似乎将所有内容完美地放入查询中,但是,我仍然得到错误的产品。例如,我要求的产品类别为“冬季”。但仍然接受类别'全季节'

的产品

这是我的代码:

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对象([query] =>数组([s] => 2457516 [post_type] =>产品)[query_vars] =>数组([s] => 2457516 [post_type] => product [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [静态] => [pagename] => [page_id] => 0 [second] => [分钟] => [小时] => [日] => 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] => [预览] = > [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] =>数组()[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] =>数组([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 [terms] => winter [operator] => IN)[1] = >数组([taxonomy] => product_cat [field] => slug [terms] =>数组([1] => all-season [2] => summer [3] => run- flat)[operator] => NOT IN)))[tax_query] => WP_Tax_Query对象([queries] => Array()[关系] => AND [table_aliases:protected] => Array()[queried_terms] => Array()[primary_table] => [primary_id_column] =&gt ;)[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:私人] => [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))

1 个答案:

答案 0 :(得分:0)

我的猜测是你的tax_query中的条件有一个双数组。你有:

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

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

这应该只是:

'terms' => $categories