【问题标题】:Remove out of stock products from WooCommerce related products custom WP query从 WooCommerce 相关产品自定义 WP 查询中删除缺货产品
【发布时间】:2021-03-23 07:41:43
【问题描述】:

您好,我想根据我的自定义查询显示相关产品,但我只想显示“in_stocks”产品,而 meta_query 不适用于 tax_query。谁能帮帮我?

 $query_args = array(
    'posts_per_page' => 10,
    'no_found_rows'  => 1,
    'post__not_in' => array( $product->get_id()),
    'post_status'    => 'publish',
    'post_type'      => 'product',
     
            'tax_query'      => array(
                'relation'      => 'OR',
                 array(
                     'taxonomy'     => 'product_cat',
                     'field'        => 'id',
                     'terms'        => $cats_array
                 ),
                array(
                    'taxonomy'     => 'product_tag',
                    'field'        => 'id',
                    'terms'        => $tags_array
                ) )
    );

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy taxonomy-terms


    【解决方案1】:

    要从自定义 WP 查询中删除缺货产品,您需要向旅游税查询添加一个额外的数组,如下所示:

    $query_args = array(
        'posts_per_page' => 10,
        'no_found_rows'  => 1,
        'post__not_in'   => array( $product->get_id() ),
        'post_status'    => 'publish',
        'post_type'      => 'product',
        'tax_query'      => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_visibility',
                'field'    => 'name',
                'terms'    => array('outofstock'),
                'operator' => 'NOT IN'
            ),
            array(
                'relation' => 'OR',
                array(
                    'taxonomy'     => 'product_cat',
                    'field'        => 'id',
                    'terms'        => $cats_array
                ),
                array(
                    'taxonomy'     => 'product_tag',
                    'field'        => 'id',
                    'terms'        => $tags_array
                )
            )
        ),
    );
    

    或者也可以通过这种方式使用元查询:

    $query_args = array(
        'posts_per_page' => 10,
        'no_found_rows'  => 1,
        'post__not_in'   => array( $product->get_id() ),
        'post_status'    => 'publish',
        'post_type'      => 'product',
        'tax_query'      => array(
            'relation' => 'OR',
            array(
                'taxonomy'     => 'product_cat',
                'field'        => 'id',
                'terms'        => $cats_array
            ),
            array(
                'taxonomy'     => 'product_tag',
                'field'        => 'id',
                'terms'        => $tags_array
            )
        ),
        'meta_query'    => array(
            'key'     => '_stock_status',
            'value'   => 'outofstock',
            'compare' => '!='
        )
    );
    

    它应该可以工作。

    相关:Show only WooCommerce in stock products with a WP_Query

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 2022-10-09
      相关资源
      最近更新 更多