query - How to display liked posts of current user in wordpress?

admin2025-06-05  2

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.

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.

Share Improve this question edited Dec 11, 2018 at 9:00 Gazi asked Dec 11, 2018 at 8:11 GaziGazi 1014 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749109630a316441.html

最新回复(0)