I have an ajax like posts system when a user like a post the system added a post meta to the post and it's named (post_liked_users). Example of post_likes_users meta value: a:1:{s:6:"user-1";i:1;}

Now how can get the users that like the post?

Note: I can't use foreach only like Jack Johansson answer I must use WP_User_Query or WP_Query because I want to use offest and number.

  • @JackJohansson To the post's meta
    – Trello
    Nov 27 '17 at 5:42

It appears that you are storing the data in the post's meta, not user's meta. Therefore, you need to retrieve it by directly getting the meta for that post, not by doing a user query.

Let's say your post's ID is $post_id:

First, retrieve the meta by get_post_meta():

$users = get_post_meta ( $post_id, 'post_liked_users', false );

I assumed that you are storing the data as multiple serialized ( based on your question ), so I set the last parameter of the above function to false, which will retrieve every meta, and we will try to extract the array:

if ( $users ) {
    foreach ( $users as $user ) {
        // $user[0] is the user-1 string here, and 
        // $user[1] is the integer ( their ID )
        echo get_user_by( 'id', $user[1] )->display_name;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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