Sort by Post View Count
-
Hi,
I’m trying to sort the posts by most popular. I want to sort by the amount of page views so that the posts with the most views show up first.
I am not sure what to put into these fields – http://prnt.sc/ely3fa
-
Thanks for your post.
If you used meta field for store post view count you can use that meta key under
Post query orderby > Meta value(Number)
And put the meta key under meta_key fieldPlease see the attachment
You don;t need use meta query parameter
Regards
Thank you for your reply.
Still not working π
I’m trying to get it to work here – http://belt.life/test-popular-posts/
Guys you didn’t understand clearly.
i can see you put
view_countin meta key field that should be different that provide 3rd party plugin.you will need to use 3rd party plugin for view count that store “View count” under post meta field.
Regards
Ahh okay I think I understand. Do you have a plugin you would recommended?
Let me search for that.
Regards
Thank you so much. I really appreciate it. If you can find a plugin and then let me know which value to enter http://prntscr.com/em3enn I will be so happy! Thank you.
Hi, i haven’t found any plugin that store view count under post meta,
Please follow the step:
You need to add following code to your theme functions.php
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); add_action('wp_head','setPostViews'); function setPostViews() { if(is_singular()){ $postID = get_the_id(); $count_key = 'post_views_count'; $count = (int)get_post_meta($postID, $count_key, true); $count +=1; update_post_meta($postID, $count_key, $count); } }And then visit some of your post,
AT post grid settigns page as i told before please usepost_views_countas meta_keyPlease follow the screenshot
you can also display this view count at post grid via layout editor
Regards
Okay great! I hope this will work but actually when I add the code to functions.php I get an error.
This is all of the code from the functions.php child theme
<?php add_action( 'wp_enqueue_scripts', 'my_enqueue_assets' ); function my_enqueue_assets() { wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' ); } remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); add_action('wp_head','setPostViews'); function setPostViews() { if(is_singular()){ $postID = get_the_id(); $count_key = 'post_views_count'; $count = (int)get_post_meta($postID, $count_key, true); $count +=1; update_post_meta($postID, $count_key, $count); } }When I update I get this error – http://prntscr.com/em40gz
Ohh wait! I added it to the parent theme and it’s working!!
Thank you so much! Amazing support.
Glad to see that help you.
If you love the support and our plugins please submit us five-star reviews at ww.wp.xz.cn plugin page https://ww.wp.xz.cn/support/plugin/post-grid/reviews/
Regards
Already did! π
Cheers!
The topic ‘Sort by Post View Count’ is closed to new replies.