1

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.

1
  • @JackJohansson To the post's meta
    – Trello
    Commented Nov 27, 2017 at 5:42

1 Answer 1

0

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 and acknowledge you have read our privacy policy.

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