SunnyD
SunnyD

Reputation: 23

WordPress exclude post in archive by meta-value

I have a custom archive page set up in WordPress that displays my custom posts. I would like to exclude custom posts with a specific meta value (eg. meta-value='sold' or meta-value='expired').

I've looked at the following questions and they didn't work for me here and here

Here's the code I was trying to work with but I kept getting a parse error:

function my_meta_remove($query){
    if($query->is_archive) {
        $query->set('meta__not_in', array(sold,expired);
    }
    return $query;
    }

add_action("pre_get_posts","my_meta_remove");

Upvotes: 0

Views: 1902

Answers (1)

Bojana Šekeljić
Bojana Šekeljić

Reputation: 1056

I guess you re missing )

change

$query->set('meta__not_in', array(sold,expired);

to this

$query->set('meta__not_in', array(sold,expired));

EDIT

meta__not_in doesn't exist, instead you need to use comparison and keys for each meta field (insert instead of key_name)

function my_meta_remove($query){
if($query->is_archive) {
    $query->set( 'meta_query', array(

        array(
              'key' => 'key_name',
              'value' => 'sold',
              'compare' => 'NOT LIKE'
        ),
        array(
              'key' => 'key_name',
              'value' => 'expired',
              'compare' => 'NOT LIKE'
        )

    ));
}
return $query;
}

add_action("pre_get_posts","my_meta_remove");

Upvotes: 2

Related Questions