【问题标题】:Problem with filter woocommerce, filter by tags, not in and taxonomy过滤woocommerce的问题,按标签过滤,而不是分类
【发布时间】:2022-01-06 05:36:15
【问题描述】:

我正在尝试进行查询,在其中输入页面上已有的产品代码、我对过滤感兴趣的标签代码和分类,但问题是它抛出了我剔除按分类但未按标签过滤的产品,或者更好的是标签是错误的,您认为可能是什么问题?

$terms = get_term_by('name', $_GET['category'], 'product_cat');
            $args = array(
                'post_type' => 'product',
                'post_status' => 'publish',
                'post__not_in' => $_GET['products'],
                'posts_per_page' => $_GET['n_products'],
                'orderby' => 'rand',
                'tag__in' => $_GET['tag_ids'],
                'tax_query' => array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field'    => 'term_id',
                        'terms'     =>  $terms->term_id,
                        'compare' => '=',
                    ),
                )
            );
            $query = new WP_Query($args);

【问题讨论】:

    标签: wordpress woocommerce filter tags custom-taxonomy


    【解决方案1】:

    尝试改变。

    'taxonomy' => 'product_cat', 
    

    'taxonomy' => 'product_tag',
    

    试试下面的代码。

    $terms = get_term_by('name', $_GET['category'], 'product_cat');
    $product_tag = get_term_by('name', $_GET['category'], 'product_tag');
    
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'post__not_in' => $_GET['products'],
        'posts_per_page' => $_GET['n_products'],
        'orderby' => 'rand',
        'tag__in' => $_GET['tag_ids'],
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'term_id',
                'terms'     =>  $terms->term_id,
                'compare' => '=',
            ),
             array(
                'taxonomy' => 'product_tag',
                'field'    => 'term_id',
                'terms'     =>  $product_tag->term_id,
                'compare' => '=',
            ),
        )
    );
    $query = new WP_Query($args);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 2015-07-15
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      相关资源
      最近更新 更多