0

Im using an ajax like dislike post system on my wordpress theme.

When the user likes the post there are some post metas to update or to add like the following:

if( $post_like == "like" ){
                update_post_meta($post_id, "likes_count", ++$meta_likes_count);
                                //add_post_meta($current_user_id, "liked_by_user_id", ++$meta_liked_by_user, false);
                                add_post_meta($post_id, "liked_by_user_id", get_current_user_id(), false);// Fixed - getting correct user id 
            } else {
                update_post_meta($post_id, "dislikes_count", ++$meta_dislikes_count);
            }

When the user likes a post automatically the code adds a new custom field in the post called liked_by_user_id with the user ID as a value.

Until here everything works as I want but the below $args don't list the posts liked by the current user in a custom page template.

$args = array(
  'meta_key' =>'liked_by_user_id',
  'post_type'  => 'post', //or a post type of your choosing
  'posts_per_page' => 30,
  'orderby' => 'date',
  'paged' => $paged,
  //'post__in' => $current_user,
  'relation' => 'OR',
  'meta_query' =>
          array(
              'key' => 'liked_by_user_id',
              'value' => $current_user,
              'type' => 'numeric',
              'compare' => '>'
          ),

  );

I don't know what I'm doing wrong here but the page shows same videos for all users.

Any one can help me out on this? Thank you.

1

Try the below $args instead, the one you have right now is looking for liked_by_user_id > than the current user id.

$args = array(
    'post_type'  => 'post', 
    'posts_per_page' => -1,
    'orderby' => 'date',
    'meta_query' => array(
        array(
            'key' => 'liked_by_user_id',
            'value' => get_current_user_id()
        )
    ),
);

The above $args should return all posts by liked by currently logged in user.

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.