Maddin February 2016

Comparing meta_query with array does not work

In my database I have the following entry at order_products:

a:1:{i:0;s:6:"1234";}

I try to show only orders with the product_id 1234:

$orders = array( 
    'post_type' => 'orders',
    'year' => $year,
    'monthnum' => $month,
    'day' => $day_minus,
'meta_query' => array (
            array (
                'key' => 'order_status',
                'value' => array( 'bill', 'back', 'pay', 'completed' ),
                'compare' => 'IN'
             ),
        array (
                'key' => 'order_products',
                'value' => array('1234'),
                'compare' => '='
            )
        )
  ); 

$the_query = new WP_Query( $orders );

$total_orders = $the_query->found_posts;

With

compare => 'LIKE' 

it is working but if there are more than the single "1234" product, these orders will showing as well (for example with product 1234, 1235 and 1256). But I only want to show it if it is alone.

I think the issue is with comparing the array.

compare => "=" 

Is there a way to compare a single item from an array with "=" ?

Answers


Sajid Anwar February 2016

Give value as NUMERIC form 'value' => array ( 79, 200, 124) example below

 //Note check relation with comment and uncomment form
 $args => array(
'relation' => 'AND',   
array (
            'key' => 'order_status',
            'value' => array( 'bill', 'back', 'pay', 'completed' ),
            'compare' => 'IN'
   ),

array(
    'key' => 'order_products',
    'value' => array ( 79, 200, 124)
    'compare' => '=',
    'type' => 'NUMERIC'

) )

any problem Welcome.

Post Status

Asked in February 2016
Viewed 2,487 times
Voted 13
Answered 1 times

Search




Leave an answer