Displaying Post Views in WordPress Without Plugins

In this article, we will show you how to display post views in WordPress without using any WordPress plugin to potentially increase user engagement by indicating the most popular posts or any other custom post type on your WordPress website.

Video of displaying post views in WordPress

The pros and cons of displaying post views on WordPress

How to Boost User Engagement on Your WordPress Site?
Increasing user engagement in WordPress through displaying post views

Displaying post views on WordPress can provide valuable insights into the popularity and engagement of your content. However, it’s essential to weigh the potential benefits against potential drawbacks before making a decision.

Pros of displaying post views:

  • Engagement Metrics: Post views can serve as a quick metric to gauge the popularity of your content. A high view count can indicate that your topic is resonating with your audience.
  • Content Optimization: By tracking which posts receive the most views, you can identify popular topics and adjust your content strategy accordingly. This can help you create more engaging and relevant content.
  • Social Proof: Displaying post views can provide social proof, suggesting that your content is well-received and trustworthy. This can encourage visitors to engage with your content further, such as leaving comments or sharing it on social media.

Cons of displaying post views:

  • Vanity Metric: While post views can be a useful metric, they should not be the sole measure of content success. Other factors, such as engagement, conversions, and time on site, are also important to consider.
  • Clickbait Encouragement: If used improperly, displaying post views can encourage clickbait tactics, where content is sensationalized or misleading to attract more views. This can harm your website’s reputation and user experience.
  • Technical Challenges: Implementing post view tracking can sometimes be technically challenging, especially for larger websites. It may require additional plugins or code modifications.

Ultimately, the decision to display post views on your WordPress website depends on your specific goals and priorities. If you believe that tracking post views will help you create better content and engage your audience more effectively, it can be a valuable tool. However, it’s important to use this metric with caution and consider the potential drawbacks.

Why reduce the number of WordPress plugins?

10 Proven Ways to Speed Up Your WordPress Site Effectively with a video tutorial
Learn how to speed up your WordPress website and optimize it up to 100%.

Overloading your WordPress website with unnecessary plugins can significantly impact its performance and security. Too many plugins can slow down your site’s loading speed, which can lead to a decrease in user engagement and search engine rankings or SEO. Additionally, plugins can introduce vulnerabilities into your website, making it more susceptible to hacking attempts. By carefully evaluating and removing unused plugins, you can improve your website’s speed, enhance its security, and ultimately provide a better user experience.

Add this php code to your functions.php file

Navigate to your WordPress theme’s directory and find the functions.php file. Then, paste the customSetPostViews() function, which you can see below, into the functions.php file.

function customSetPostViews($postID) {
    $countKey = 'post_views_count';
    $count = get_post_meta($postID, $countKey, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $countKey);
        add_post_meta($postID, $countKey, '1');
    }else{
        $count++;
        update_post_meta($postID, $countKey, $count);
    }
}

Modify single.php

Open your single.php file. Then, add the following line at the very beginning:

<?php customSetPostViews(get_the_ID()); ?>

Adding another code to single.php

The customSetPostViews() function is called at the beginning of single.php by the above one-line code to increment the view count for the current post.

The code to display the view count is added to your single.php file where you want it to appear, typically above the post content. Find the location where you want the view count to appear and add the following code:

<?php $post_views_count=get_post_meta(get_the_ID(),'post_views_count',true);if(!empty($post_views_count)){echo $post_views_count;} ?> Views

Understanding the code snippets

The provided code snippets allow you to track and display post views in WordPress without relying on any third-party plugins. This method is beneficial for those who prefer a lightweight and customizable approach.

Breakdown of the php code of customSetPostViews() function

  • This function is added to your functions.php file.
  • It checks if the post_views_count meta key exists for the current post.
  • If it doesn’t exist, it initializes the count to 1.
  • If it exists, it increments the count by 1.

Customization

  • Formatting: You can customize the output of the view count by modifying the HTML around the echo $post_views_count; line. For example, you could add a label or a different font style.
  • Conditional Display: If you only want to display the view count for certain post types or categories, you can add conditional logic to check these criteria before displaying the count.

Additional considerations

  • Caching: If you’re using caching plugins, make sure they are compatible with custom meta fields and that they update the cache when the view count changes.
  • Performance: While this method is generally efficient, for high-traffic websites, you might consider using a more optimized solution or caching the view count.

By following these steps and customizing the code as needed, you can effectively display post counts in your WordPress website without relying on external plugins.

Leave a Comment