Php 从WooCommerce相关产品自定义WP查询中删除缺货产品

Php 从WooCommerce相关产品自定义WP查询中删除缺货产品,php,wordpress,woocommerce,custom-taxonomy,taxonomy-terms,Php,Wordpress,Woocommerce,Custom Taxonomy,Taxonomy Terms,您好,我想根据我的自定义查询显示相关产品,但我只想显示“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',

您好,我想根据我的自定义查询显示相关产品,但我只想显示“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查询中删除缺货产品,您需要向tour tax查询添加一个附加数组,如下所示:

$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' => '!='
    )
);
它应该会起作用

相关的: