0

i have a user_meta for users that them is array like below:

user_saved_posts = [31289,31482,27641]

and i want to get users that their user_meta include an item of an array like below:

goal_posts = [31289,31422,77641,41289,21482,17641]

if user have an item of goal_posts array must returned them. i use below code but this code worked if i have a value for search in user_meta

$args = [
        'meta_query' => [
            [
                [
                    'key' => 'saved_posts',
                    'value' => sprintf(':"%s";', 31289),
                    'compare' => 'LIKE'
                ]
            ]
        ]
    ];
    get_users($args);
1
  • For this kind of purpose, you shouldn't save your meta as a single array. As default, WordPress saves the meta in multiple fields, unless you explicitly say not to. When you're adding user meta, set the unique parameter to false, and then your code will work. Note that in this way you have 3 user_saved_posts meta instead of a single array. Let me know if you needed any more help.
    – Amirition
    Commented May 10, 2021 at 15:06

1 Answer 1

0

I solved my problem in another way(WPDB). I wrote a query for this purpose like below:

$goal_posts = [31289,31422,77641,41289,21482,17641];
$sql = "SELECT * FROM wp_usermeta WHERE (meta_key = 'saved_posts') AND (";
$sql .= implode(" OR ", $goal_posts);
$sql .= ') GROUP BY user_id';
global $wpdb;
$users = $wpdb->get_results($sql);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.