I am having trouble to query cpt for the current user if s/he is in the meta value array.
query args
$args = [
'post_type' => $cp::get_module_cpt(),
'posts_per_page' => - 1,
'meta_query' => [
'relation' => 'AND',
[
'key' => 'premium_module',
'value' => 0,
'compare' => '=',
],
[
'key' => 'permit_users',
'value' => get_current_user_id(),
'compare' => 'IN',
],
],
];
meta key
permit_users
meta value
if has value
Database
a:5:{i:0;s:2:"23";i:1;s:1:"2";i:2;s:1:"6";i:3;s:1:"7";i:4;s:2:"27";}
In PHP
Array
(
[0] => 29
[1] => 28
...
)
if has no value
return false or empty array. Depends if ever set the field before.
additionally,
I have tried setting compare to =
equal but that also didn't work. Probably since the value is in array.
Result Looking
The query should return all posts that have current user id in the permit_users
key.
Important:
Thepermit_users
is an optional, so if the field has not value (false) or empty array, the query should ignore it.
Update
I have tried compare with LIKE
that seems working. But is it good to compare serialized data? What if current user is 11
and value has 111
user in it?
permit_users
– gvgvgvijayanLIKE
that seems working but what about if field is not set or empty? And searching in serialized data usingLIKE
is a good? What will happen if the current user id is11
and the meta value has user id111
? I am updating question with db value. – Code Lover