skip to Main Content

I have this query in WP_Query

$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(
'key'     => 'pre_orders_enabled',
'value'   => 'no',
'compare' => '=',
), ),
) );

This shows me products in preorder with the value "no"

The problem is that I also have products that don’t have the "pre_orders_enabled" meta and I need to include them in the WP_Query

In short, I want to display the products that have the value no in the meta pre_orders_enabled and the products that do not have the meta pre_orders_enabled

2

Answers


  1. $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',
            ),
        ),
    ) );
    
    Login or Signup to reply.
  2. $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(
                'relation' => 'OR',
                array(
                    'key'     => 'pre_orders_enabled',
                    'compare' => 'NOT EXISTS',
                ),
                array(
                    'key'     => 'stock_status',
                    'value'   => array( 'instock', 'outofstock' ),
                    'compare' => 'IN',
                ),
            ),
        ),
    ) );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search