Meta query ignores multiple values of the key

Question

I have the following post meta for posts:

_post_allowed_countries

//Example value: 
array ( 0 => 'en', 1 => 'gr', )

// A list of allowed countries

and

_post_allowed_countries_not

//Example value: 
array ( 0 => '0' )

// True/False -> If true ( '1' ), _post_allowed_countries act as disallowed countries

and this is my meta query:

array(
    'relation'              => 'OR',
    array(
        'relation'          => 'AND',
        array(
            'relation'      => 'OR',
            array(
                'key'       =>'_post_allowed_countries_not',
                'value'     => '',
                'compare'   => 'NOT EXISTS'
            ),
            array(
                'key'       =>'_post_allowed_countries_not',
                'value'     => '0',
                'compare'   => '='
            ),
        ),
        array(
            'key'       =>'_post_allowed_countries',
            'value'     => serialize('gr'),
            'compare'   => 'LIKE'
        )
    ),
    array(
        'relation'      => 'AND',
        array(
            'key'       =>'_post_allowed_countries_not',
            'value'     => '1',
            'compare'   => '='
        ),
        array(
            'key'       =>'_post_allowed_countries',
            'value'     => serialize('gr'),
            'compare'   => 'NOT LIKE'
        )
    ),
)

So basically I am trying to query all the posts that have gr as one of their allowed counties or they don’t have gr as one of their disallowed countries ( gr in reality is the current’s visitor country code ).

The problem seems to be that the meta query only checks for the first value of _post_allowed_countries. Is there any way to fix this?

Note: Yes I know it’s easier to do this with taxonomies but at this point it’s not option for me. I need to make this work with post meta.

0
DimChtz 4 months 0 Answers 15 views 0

Leave an answer