SHARE
TWEET

display wordpress posts sorted by post view count

pustakakoran Apr 19th, 2017 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Steps:
  2.  
  3. 1. Paste the code below into your current theme’s function.php file. Try to use child theme if you are not already having one.(Child theme is optional though)
  4.  
  5. /**
  6.  * To display number of posts.
  7.  *
  8.  * @param $postID current post/page id
  9.  *
  10.  * @return string
  11.  */
  12. function subh_get_post_view( $postID ) {
  13.     $count_key = 'post_views_count';
  14.     $count     = get_post_meta( $postID, $count_key, true );
  15.     if ( $count == '' ) {
  16.         delete_post_meta( $postID, $count_key );
  17.         add_post_meta( $postID, $count_key, '0' );
  18.  
  19.         return '0 View';
  20.     }
  21.  
  22.     return $count . ' Views';
  23. }
  24.  
  25. /**
  26.  * To count number of views and store in database.
  27.  *
  28.  * @param $postID currently viewed post/page
  29.  */
  30. function subh_set_post_view( $postID ) {
  31.     $count_key = 'post_views_count';
  32.     $count     = (int) get_post_meta( $postID, $count_key, true );
  33.     if ( $count < 1 ) {
  34.         delete_post_meta( $postID, $count_key );
  35.         add_post_meta( $postID, $count_key, '0' );
  36.     } else {
  37.         $count++;
  38.         update_post_meta( $postID, $count_key, (string) $count );
  39.     }
  40. }
  41.  
  42. /**
  43.  * Add a new column in the wp-admin posts list
  44.  *
  45.  * @param $defaults
  46.  *
  47.  * @return mixed
  48.  */
  49. function subh_posts_column_views( $defaults ) {
  50.     $defaults['post_views'] = __( 'Views' );
  51.  
  52.     return $defaults;
  53. }
  54.  
  55. /**
  56.  * Display the number of views for each posts
  57.  *
  58.  * @param $column_name
  59.  * @param $id
  60.  *
  61.  * @return void simply echo out the number of views
  62.  */
  63. function subh_posts_custom_column_views( $column_name, $id ) {
  64.     if ( $column_name === 'post_views' ) {
  65.         echo subh_get_post_view( get_the_ID() );
  66.     }
  67. }
  68.  
  69. add_filter( 'manage_posts_columns', 'subh_posts_column_views' );
  70. add_action( 'manage_posts_custom_column', 'subh_posts_custom_column_views', 5, 2 );
  71.  
  72.  
  73. 2. Open single.php file which is responsible to display each post in WordPress and paste this single line of code inside the loop
  74.  
  75. <?php subh_set_post_view( get_the_ID() ); ?>
  76.  
  77. 3. You can display the post view count by using this line of code inside the WordPress loop on any page.
  78.  
  79. <?php echo subh_get_post_view(get_the_ID()); ?>
  80.  
  81. 4. We can display posts sorted by Post view counts by using the code below.
  82.  
  83. <?php
  84. $args    = array(
  85.     'numberposts' => 4,  /* get 4 posts, or set -1 to display all posts */
  86.     'orderby'     => 'meta_value',  /* this will look at the meta_key you set below */
  87.     'meta_key'    => 'post_views_count',
  88.     'order'       => 'DESC',
  89.     'post_type'   => 'post',
  90.     'post_status' => 'publish'
  91. );
  92. $myposts = get_posts( $args );
  93. foreach ( $myposts as $mypost ) {
  94.     /* do things here */
  95. }
  96. ?>
  97.  
  98. WordPress plugins for post view counts are good but those provide extra functionalities which are not really needed for most of the cases; so why to bloat the setup and add to the response load of the website. Fastness of website matters !!
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top