最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far