how to get users with usermeta include array of an array

The question:

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);

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment