Home > Mobile >  Querry sql wordpress args
Querry sql wordpress args

Time:06-13

Hello I have this query in wordpress

        $args['orderby'] = 'rand';
        $args['order'] = '';
        $args['meta_key'] = '_wc_pre_orders_enabled';
        $args['meta_value'] = 'no';
        $args['meta_compare'] = '=';

What I need is that wordpress filter by the meta key _wc_pre_orders_enabled and that the value is no or null

Just display those results but currently it doesn't work. What am I doing wrong?

Edit: I also tried this way but it's not working. Apparently meta_query is not happening.

      $args = array(
        'orderby' => 'rand',
        'order' => '',
        'meta_query' => array(
          array(
            'key' => '_wc_pre_orders_enabled',
            'value' => array('no',''),
            'compare' => 'IN'
          )
        )
      );

CodePudding user response:

I think what you're asking for is something like this?

$args = array(
    'post_type'  => 'product',
    'orderby'    => 'rand',
    'meta_query' => array(
        'relation' => 'and',
        array(
            'relation' => 'OR',
            array(
                'key'     => '_wc_pre_orders_enabled',
                'value'   => 'no',
                'compare' => 'LIKE',
            ),
            array(
                'key'   => '_wc_pre_orders_enabled',
                'value' => null,
            ),
        ),
    ),
);

$query = new WP_Query( $args );

Which produces this SQL

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID
FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1  AND (
    (
        ( wp_postmeta.meta_key = '_wc_pre_orders_enabled' AND wp_postmeta.meta_value LIKE 'no' )
        OR
        ( wp_postmeta.meta_key = '_wc_pre_orders_enabled' AND wp_postmeta.meta_value = '' )
    )
    ) AND ((wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')))
GROUP BY wp_posts.ID
ORDER BY RAND()
    LIMIT 0, 10
  • Related