How to Count Post Views and show in post page in WordPress?
Tracking post views on your WordPress site can provide valuable insights into your content’s popularity and audience engagement. In this blog post, we’ll explore two methods to count post views and display the count on your post page: using custom code and using a plugin. We’ll walk you through each method step by step, with examples and explanations of every function involved.
Method 1: Using Custom Code
Adding custom code to your theme is a powerful way to track and display post views. Here’s how to do it:
Step 1: Add Code to Your Theme’s functions.php File
First, we need to add some code to our theme’s functions.php file to handle counting and displaying post views.
- Open functions.php File:
- Go to your WordPress dashboard, navigate to Appearance > Theme Editor, and select the functions.php file.
- Add the Following Code:
Function to track post views
// Function to track post views function set_post_views($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if ($count == '') { $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); } else { $count++; update_post_meta($postID, $count); } }
This above code sets and updates the post views count. It checks if the post_views_count
meta key exists for the given post. If not, it initializes it to 0. Otherwise, it increments the count.
Function to display post views
// Function to display post views function get_post_views($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if ($count == '') { return "0 Views"; } return $count . ' Views'; }
This above code retrieves the post views count for the given post ID. If the count does not exist, it returns “0 Views”; otherwise, it returns the current count.
Function to set views when a post is viewed
// Function to set views when a post is viewed function track_post_views($post_id) { if (!is_single()) return; if (empty($post_id)) { global $post; $post_id = $post->ID; } set_post_views($post_id); } add_action('wp_head', 'track_post_views');
This above code is hooked to wp_head and sets the views count when a single post is viewed. It checks if the current page is a single post and then calls set_post_views.
Function to add the views column to the admin posts list
// Function to add the views column to the admin posts list function add_post_views_column($columns) { $columns['post_views'] = 'Views'; return $columns; } add_filter('manage_posts_columns', 'add_post_views_column');
The above function adds a “Views” column to the admin posts list Screen.
Function to display the views in the admin posts list
// Function to display the views in the admin posts list
function display_post_views_column($column_name, $postID) { if ($column_name === 'post_views') { echo get_post_views($postID); } } add_action('manage_posts_custom_column', 'display_post_views_column', 10, 2);
This function displays the views count in the newly added “Views” column in the admin posts list.
Step 2: Display Post Views in Your Template
Next, we need to display the post views count in our post template.
- Open the Single Post Template:Go to
Appearance > Theme Editor
, and selectsingle.php
(or another appropriate template file). - Add the Following Code Where You Want to Display the Views Count:
echo get_post_views(get_the_ID());
This code will output the views count for the current post.
So our final code would be as follows.
// Function to track post views function set_post_views($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if ($count == '') { $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); } else { $count++; update_post_meta($postID, $count); } } // Function to display post views function get_post_views($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if ($count == '') { return "0 Views"; } return $count . ' Views'; } // Function to set views when a post is viewed function track_post_views($post_id) { if (!is_single()) return; if (empty($post_id)) { global $post; $post_id = $post->ID; } set_post_views($post_id); } add_action('wp_head', 'track_post_views'); // Function to add the views column to the admin posts list function add_post_views_column($columns) { $columns['post_views'] = 'Views'; return $columns; } add_filter('manage_posts_columns', 'add_post_views_column'); // Function to display the views in the admin posts list function display_post_views_column($column_name, $postID) { if ($column_name === 'post_views') { echo get_post_views($postID); } } add_action('manage_posts_custom_column', 'display_post_views_column', 10, 2);
Output in Post Screen:
Output in Blog Page
Method 2: Using a Plugin
If you prefer not to add custom code, you can use a plugin to count and display post views.
Step 1: Install and Activate a Plugin
- Install the Plugin:Go to your WordPress dashboard, navigate to
Plugins > Add New
, and search for “Post Views Counter”. Install and activate the plugin. - Configure the Plugin:After activating, go to
Settings > Post Views Counter
to configure the plugin settings according to your needs.
Step 2: Display Post Views
- Display Views Using a Shortcode:The plugin may provide a shortcode to display the post views. For example, if using the “Post Views Counter” plugin, you can use:
echo pvc_post_views(get_the_ID());
- Display Views in the Template:
Add the above shortcode or function in your post template file (e.g., single.php) where you want to display the post views count.
Conclusion
Both methods are effective for counting and displaying post views in WordPress. Using custom code gives you more control and customization options, while a plugin provides a simpler, user-friendly solution. Choose the method that best fits your needs and technical comfort level.