簡體   English   中英

從WooCommerce相關產品定制WP查詢中去除缺貨產品

[英]Remove out of stock products from WooCommerce related products custom WP query

您好,我想根據我的自定義查詢顯示相關產品,但我只想顯示“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
                ) )
    );

要從自定義 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' => '!='
    )
);

它應該工作。

相關: 僅顯示帶有 WP_Query 的庫存產品中的 WooCommerce

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM