Exclude / Not equal multiple values in wordpress query

I have modified a WordPress query.

I need to exclude three values but cannot figure out how to do it. I have read lots of people passing more than one value to match but not to exclude. I have tried several things across several hours.

Read More
$popularpost = new WP_Query( array( 'posts_per_page' => '12', 
'cat' => $cat_id,
'posts_type' => 'post',
'paged' => $paged, 
'meta_key' => 'post_price', 
'meta_value' =>'SWAP', 'FREE', 'WANTED',
'meta_compare' => '!=',

Any help is greatly appreciated.

Related posts

2 comments

  1. I was able to get this working, heres the code I used in case anyone is interested

        $popularpost = new WP_Query( array( 'posts_per_page' => '12', 
                            'cat' => $cat_id,
                            'posts_type' => 'post',
                            'paged' => $paged, 
                            'meta_key' => 'post_price', 
                            'meta_query' => array(
        array(
            'key' => 'post_price', 
            'value' => 'SWAP',
            'compare' => '!='
    
        ),
        array(
            'key' => 'post_price', 
            'value' => 'FREE',
            'compare' => '!='
    
        ),
        array(
            'key' => 'post_price', 
            'value' => 'WANTED',
            'compare' => '!='
    
        )
        ),
        ) );    
    
  2. There is an issue here:

    'meta_value' =>'SWAP', 'FREE', 'WANTED',
    

    In this case meta_value is set to SWAP. FREE and WANTED are just separate entries in the array. Writing your code out like this reveals the problem:

    array( 
        'posts_per_page' => '12', 
        'cat' => $cat_id,
        'posts_type' => 'post',
        'paged' => $paged, 
        'meta_key' => 'post_price', 
        'meta_value' => 'SWAP', 
        'FREE', 
        'WANTED',
        'meta_compare' => '!=',
        ...
    )
    

    I think this is what you want:

    'meta_value' => array('SWAP', 'FREE', 'WANTED'),
    

Comments are closed.