Technology

Integrating WP-PostViews to Track View Counts Across All Posts

Learn to integrate WP-PostViews plugin for tracking post view counts on various WordPress pages like archive.php, category.php, index.php, ensuring comprehensive analytics across your site.

Sangrak

Among the plugins available for WordPress, there's WP-PostViews, designed to count the views of each post. Typically, the plugin counts views in the single.php or is_page() mode when a post is opened. However, the view count is also visible across other pages such as archive.php, category.php, index.php (hereafter referred to as 'pages displaying multiple posts'), showcasing summarized content which then leads to the full content page upon clicking the title.

Unlike the typical WordPress skins that only show summarized content on 'pages displaying multiple posts', I've set my WordPress to display the full content on these pages, similar to the approach used by Tattertools. This setting, while operational, does not increase the view count when navigating across multiple posts. To counter this, I've customized the plugin to increase view counts on 'pages displaying multiple posts'. Here's how to modify it.

How to Modify

File to Modify: /wp-content/plugins/wp-postviews/wp-postviews.php

Original

if(!wp_is_post_revision($post)) {
	if(is_single() || is_page()) {
		$id = intval($post->ID);
		$views_options = get_option('views_options');
		post_views = get_post_custom($id);

Modification

if(!wp_is_post_revision($post)) {
	$id = intval($post->ID);
	if($id) {
		$views_options = get_option('views_options');
		post_views = get_post_custom($id);

This modification ensures that viewing numbers are tallied across a wider range of page types, allowing for more comprehensive tracking and analytics for your WordPress content.

More on this topic