admin管理员组文章数量:1336632
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
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.
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
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.
Share Improve this question asked May 21, 2020 at 11:29 pixelngrainpixelngrain 1,3901 gold badge23 silver badges50 bronze badgesImportant:
Thepermit_users
is an optional, so if the field has not value (false) or empty array, the query should ignore it.
1 Answer
Reset to default 2You can't easily query inside arrays inside a meta value. This is because there's no such thing as an array value in MySQL. Instead the value is stored as a serialized string.
So an array like this:
array(
0 => 29,
1 => 28,
);
Is stored in the database as this string:
a:2:{i:0;i:29;i:1;i:28;}
So the only way to find values inside it is to do a LIKE
on the string. But the problem is that if you want to check if the value 28
is in the array, you'd need to query LIKE '%:28;%'
, but apart from being much slower, could give you incorrect results if 28
is an index in the array.
So instead of storing an array, use add_post_meta()
to add multiple rows for the same meta key. For example, if you use:
update_post_meta( $post_id, 'permit_users', [ 29, 28 ] );
You will get:
+--------------+--------------------------+
| meta_key | meta_value |
+--------------+--------------------------+
| permit_users | a:2:{i:0;i:29;i:1;i:28;} |
+--------------+--------------------------+
But if you use:
add_post_meta( $post_id, 'permit_user', 28 );
add_post_meta( $post_id, 'permit_user', 29 );
You will get:
+-------------+------------+
| meta_key | meta_value |
+-------------+------------+
| permit_user | 28 |
| permit_user | 29 |
+-------------+------------+
And your WP_Query
using IN
will work the way you expect:
[
'key' => 'permit_user',
'value' => get_current_user_id(),
'compare' => 'IN',
],
本文标签: queryHow to check current user in meta value array in WPQuery metaquery
版权声明:本文标题:query - How to check current user in meta value array in WP_Query meta_query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742412875a2470140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论