Logo
Entries about technology. Website-related news. Projects that I'm working on. Code, web services and more. Programming insights and help. Space
Space

Track your WordPress post views.

Posted on May 04, 2010 by Jimmy K. in Tutorials
I wanted to be able to track the number of views each one of my posts gets in a very general way so I could create a “popular articles” section on my blog. I looked through the MySQL database and couldn’t find any traces of WordPress tracking post views by default. So! What I did was write a nifty little set of functions that I’m pretty sure don’t follow any development standards.

Update: I’m slowly tackling the obstacle of converting this tutorial into a WordPress plugin.

First, I created a table in the MySQL database to keep track of the post views and I update the table every time a post is loaded in the browser. I only count specific post views (where ./?p=XYZ) is in the URL.

Here is the MySQL statement to create the table:
CREATE TABLE IF NOT EXISTS `wp_postviews_z` (`PostID` mediumint(8) unsigned NOT NULL, `NumViews` mediumint(8) unsigned NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Then I wrote a function called jTrackPostView(), which basically updates the database whenever a post is being viewed. I placed this code into the index.php file. All it really does is says: if $_GET['p'] is set, call the jTrackPostView() function because we only want to track the post view if we’re actually reading the entire post (not the excerpt).
<?php print('This is PHP.'); ?>
To display the popular posts, I placed this code into the sidebar.php file:
<table style="width: 250px;">

<?php

$aViewCounts = array();
$oViewsQuery = mysql_query("SELECT `PostID`, `NumViews` FROM `wp_postviews_z` ORDER BY `NumViews` DESC LIMIT 10;") or die(mysql_error());
$sPopularQuery = "SELECT `ID`, `post_title` FROM `wp_posts` WHERE `ID` = '0'";

while ($oViewsResults = mysql_fetch_assoc($oViewsQuery)) {
$aViewCounts[$oViewsResults['PostID']] = $oViewsResults['NumViews'];
$sPopularQuery .= " OR `ID` = '" . $oViewsResults['PostID'] . "'";
}

$sPopularQuery .= " ORDER BY `post_title` ASC;";

$oQuery = mysql_query($sPopularQuery) or die(mysql_error());

$y = 0; while ($oResults = mysql_fetch_assoc($oQuery)) {
print('<tr style="width: 250px;">');
print('<td style="width: 250px;"><ul><li>');
print('<a href="./?p=' . $oResults['ID'] . '">' . $oResults['post_title'] . '</a> <span>(' . $aViewCounts[$oResults['ID']] . ')</span>');
print('</li></ul></td></tr>');
}

?>

</table>
I really want to help other people by converting this into a lightweight WordPress plugin, but I don’t know how to do that yet. Hopefully by the end of May I’ll be better equipped to tackle that. Can someone please suggest where I can find a decent tutorial for writing WordPress plugins? If so, please let me know in the comments.

Tags: , , ,
Twitter Design Bump Web Blend Digg del.icio.us Stumble Upon Facebook Design Float Reddit Evernote

Here are some random, unrelated posts.

 
 

2 Responses to "Track your WordPress post views."

Avatar
Space Space Space
Quote Posted by Jimmy K. on May 04, 2010 10:19PM.

Thanks! That is all very helpful and I will definitely keep it in mind when it's time to program my own plugin.

Should I be using the is_admin() function to make sure I'm not counting my own? I don't have open registration so I'm assuming this is what you're talking about.

I'll have to look into the wp_post_meta table and add_post_meta() function. I want to keep this as standard as possible using the built-in WordPress functions.
Space
Space Space Space
Avatar
Space Space Space
Quote Posted by Pepijn de Vos on May 04, 2010 10:09PM.

Hey,

I have quite some experience with Wordpress.

First off, I don't think you need a new table to store views for a post. You could use the wp_post_meta table for this, which means using the custom fields feature of Wordpress(add_post_meta).

It would also be wise to check if a user is logged in(is_user_logged_in) before registering a post view, you don't want to be counting your own views.

The best resource for developing Wordpress plugins I know is actually the Wordpress Codex itself.

What you'll want to do is create a php file with the correct comment section in the header for the plugin info, then have a look at hooks and widgets, especially add_action('wp') and register_sidebar_widget("Top posts", "top_posts");

Be sure to have a look at hallo.php in the plugins folder for a basic plugin sample. Also check out my stats widget plugin for some widget code. This one relies on Wordpress.com stats.

http://wordpress.org/extend/plugins/wordpresscom-stats-top-posts-sidebar-widget/
Space
Space Space Space
 
 

If you found this useful, please leave a reply.

 
 
Space
 
 
Archives
 
 
Popular Articles
 
 
 
 
Friends
 
 
Tag Cloud
Space
Space
© 2010 END[SEVEN] Web Development. All Rights Reserved.