1

I have the following query but it is still returning results which include posts which have a meta value of 'deal'.

Any Clues on what am i missing in this

    $args = array(
    'post_type' => POST_TYPE,
    'meta_key' => 'is_hot',
    'meta_value' => '1',
    'meta_compare' => '=',
    'posts_per_page' => 20,
    'no_found_rows' => true,
    'suppress_filters' => false,
    'meta_key'     => 'offer_type',
    'meta_value'   => 'deal',
    'meta_compare' => 'NOT LIKE',
     ),

);

new WP_Query( $args );

2 Answers 2

6

You can not query for multiple meta fields using meta_key and meta_value parameters, you have to use meta_query parameter. Using multiple meta_key and meta_value parameters will use only the first found pair, that is why in your code the condition about "offer_type NOT LIKE deal" is ignored.

So, the correct query should be:

$args = array(

    'post_type'        => POST_TYPE,
    'posts_per_page'   => 20,
    'no_found_rows'    => true,
    'suppress_filters' => false,
    //Se the meta query
    'meta_query'       => array(
        //comparison between the inner meta fields conditionals
        'relation'    => 'AND',
        //meta field's first condition
        array(
            'key'          => 'is_hot',
            'value'        => '1',
            'compare'      => '=',
        ),
        //meta field's second condition
        array(
            'key'          => 'offer_type',
            'value'        => 'deal',
            //I think you really want != instead of NOT LIKE, fix me if I'm wrong
            //'compare'      => 'NOT LIKE',
            'compare'      => '!=',
        )
    ),

);

$query = new WP_Query( $args );
1
  • Awesome Man. Works like a charm. Thanks for your help. Mar 17, 2015 at 12:22
1

meta_compare possible values are

'!=', '>', '>=', '<', or ='. Default value is '='

if you want to use NOT LIKE you need to create a meta_query.

example

'meta_query' => array(                  //(array) - Custom field parameters (available with Version 3.1).
   array(
     'key' => 'color',                  //(string) - Custom field key.
     'value' => 'blue'                  //(string/array) - Custom field value (Note: Array support is limited to a compare value of 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN')
     'type' => 'CHAR',                  //(string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.
     'compare' => '='                   //(string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. Default value is '='.
   ),
   array(
     'key' => 'price',
     'value' => array( 1,200 ),
     'compare' => 'NOT LIKE'
   ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.