WP_查询库存状态过滤器WordPress

jtw3ybtb  于 2023-06-21  发布在  WordPress
关注(0)|答案(2)|浏览(115)

如果_pre_orders_enabled的值不存在或值为no,我有这个WP_Query代码,它会显示结果
我想要的是,我也过滤它的_stock_status我尝试了这个,但它没有工作。
我的代码:

$products = new WP_Query( array(
'post_type' => 'product',
'starts_with' => $letra,
'posts_per_page' => -1,
'order' => 'DESC',
    'meta_query'     => array(
        'relation' => 'OR',
        array(
            'key'     => '_pre_orders_enabled',
            'value'   => 'no',
            'compare' => '=',
        ),
        array(
            'key'     => '_pre_orders_enabled',
            'compare' => 'NOT EXISTS',
        ),
    ),
) );

我试着过滤

array(
    'key'     => '_stock_status',
    'value'   => 'instock',
    'compare' => '=',
),

但是没有用

vngu2lb8

vngu2lb81#

我找到解决办法了

$products = new WP_Query( array(
    'post_type'      => 'product',
    'starts_with'    => $letra,
    'posts_per_page' => -1,
    'order'          => 'DESC',
    'meta_query'     => array(
        array(
            'key'     => '_stock_status',
            'value'   => 'instock',
            'compare' => '=',
        ),
        array(
            'relation' => 'OR',
            array(
                'key'     => '_pre_orders_enabled',
                'value'   => 'no',
                'compare' => '=',
            ),
            array(
                'key'     => '_pre_orders_enabled',
                'compare' => 'NOT EXISTS',
            ),
        ),
    ),
) );
zzoitvuj

zzoitvuj2#

You can try following code.

$products = new WP_Query( array(
    'post_type'      => 'product',
    'starts_with'    => $letra,
    'posts_per_page' => -1,
    'order'          => 'DESC',
    'meta_query'     => array(
        'relation' => 'AND', 
        array(
            'key'     => '_pre_orders_enabled',
            'value'   => 'no',
            'compare' => '=',
        ),
        array(
            'key'     => '_stock_status',
            'value'   => 'instock',
            'compare' => '=',
        ),
    ),
) );

相关问题