admin管理员组文章数量:1130349
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 badges1 Answer
Reset to default 1Try 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.
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 badges1 Answer
Reset to default 1Try 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.
本文标签: queryHow to display liked posts of current user in wordpress
版权声明:本文标题:query - How to display liked posts of current user in wordpress? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749109604a2317155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论