I have a checkbox field for a custom_post_type
called distributors
.
I am using get_posts and meta_query to filter the posts by the region
checked. Each post has a value of canada
or usa
.
In my code I get their session which defines $country
as canada
or usa
and is used in the meta_value
field of arguments.
Here is my code:
$args = array (
'post_type' => 'distributors',
'post_status' => 'publish',
'numberposts' => -1,
'meta_query' => array (
'key' => 'region',
'value' => $country,
'compare' => 'LIKE'
)
);
$distributors = get_posts($args);
It should then only show posts distributors
which has a region matching the visitor's session.
In the above case it outputs all distributor
posts.
I did a var_dump
to find out why it's not matching. In the case of a usa
related post, it shows the region
stored as:
["ID"]=>
int(13353)
["city"]=>
string(9) "Elizabeth"
["province"]=>
string(10) "New Jersey"
["region"]=>
array(1) {
[0]=>
string(3) "usa"
}
In this case, in the database the value looks like:
In the case of a canada
related post it shows region
as:
["ID"]=>
int(12840)
["city"]=>
string(7) "Concord"
["province"]=>
string(7) "Ontario"
["region"]=>
string(6) "canada"
In the database, the value looks like so:
What can I do to match when some values are NOT serialized and other are? I have a feeling some post data was imported directly and others were added via the wordpress dashboard which might account for some values not being serialized while others are.