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 an option for me. I need to make this work with post meta.
Note 2: It's not that I can't convert this to taxonomies. It's that I want to avoid taxonomies for a couple of reasons which are irrelevant to the question.
Note 3: No, I don't store serialized data for _post_allowed_countries
. The array I posted is just an example of what is stored. I actually add multiple post meta for the same key.
EDIT: After a lot of testing with 'compare' = '='
and 'compare' = '!='
I think only the !=
doesn't work as expected because it validates at the first match. In that case is it possible to tell the !=
meta query part to check for all meta values of _post_allowed_countries
key?