How to track popular posts by views in WordPress without a plugin

In the world of WordPress, understanding which posts are most popular among your users can be crucial for optimizing your content and increasing traffic. Although there are multiple plugins that facilitate this task, it is also possible to track post views without installing additional add-ons. In this article, we will explore how to do this by implementing custom code.

Implementation of a view tracking system

To track views of your WordPress posts, you will first need to add a code to the file. functions.php of your theme. This code will count and store the views of each post in the database.

Creation of the function to count views

The main function that will be responsible for counting views must be added to the file. functions.php. The following code is an example of how this can be implemented:

function contar_vistas($post_id) {
    if (!is_single()) {
        return;
    }
    $count_key = 'vistas_post';
    $count = get_post_meta($post_id, $count_key, true);
    if($count == '') {
        $count = 0;
        delete_post_meta($post_id, $count_key);
        add_post_meta($post_id, $count_key, '0');
    } else {
        $count++;
        update_post_meta($post_id, $count_key, $count);
    }
}
add_action('wp_head', 'contar_vistas');

In this code, it checks if the current page is an individual post. If so, it gets the number of views for the post and increments it each time the post is accessed.

Display the number of views on posts

Once you have implemented the view counting feature, you may want to display the number of views on the front end of your site. To do this, you can add the following code where you want the number of views to appear, such as in the file single.php:

$views = get_post_meta(get_the_ID(), 'post_views', true); echo '

Views: ' . $views .

How to track popular posts by views in WordPress without a plugin
Download our web maintenance guide Free of charge!
Free guide for freelancers and small businesses that want to avoid surprises and improve their web performance.
'

';

This code will retrieve the number of views stored in the database and display it in the post.

Track the most viewed posts

Once you have a system for counting views, the next step is to be able to display the most viewed posts. To do this, you can use a custom query to get the posts sorted by number of views.

Consultation of popular publications

Using the following code, you can track the most viewed posts and display them anywhere on your site:

function popular_posts($number_of_posts = 5) { $args = array( 'meta_key' => 'views_post', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'posts_per_page' => $number_of_posts
    ); $query = new WP_Query($args); return $query; }

This code defines a function that uses WP_Query to obtain publications sorted by number of views. The parameter posts_per_page allows you to define how many posts you want to display.

Display popular posts on the frontend

To display the most viewed posts, you can use the following code in the file where you want them to appear:

$popular_posts = popular_posts(5); if ($popular_posts->have_posts()) { echo '

Popular Publications

    ‘;
    while ($popular_posts->have_posts()) {
    $popular_posts->the_post();
    echo '

  • ' . get_the_title() . ' – ‘. get_post_meta(get_the_ID(), ‘post_views’, true). ‘ views
  • ‘;
    }
    echo '

‘;
}
wp_reset_postdata();

This piece of code displays a list of popular posts with their number of views, allowing users to easily access the most relevant content.

Performance considerations

While implementing a view tracking system without a plugin can be very effective, it is important to consider some factors that could affect your site's performance:

  • Load into the database: Each time a user views a post, a query is made to the database to update the number of views, which can generate additional load.
  • Query optimization: Ensure that your popular posts query is optimized to avoid slowing down the site, especially if you have a large volume of content.
  • Search: Consider implementing a caching system to mitigate the impact on the database and improve the site's loading speed.

Conclusion

Tracking views of your WordPress posts without using a plugin is a process that can be achieved by implementing custom code. Through this method, you can gain valuable insights into what content resonates most with your audience, allowing you to optimize your content strategy and improve the user experience on your site.