I am using Stattraq to keep track of the traffic on this weblog. I installed it back in August and recently installed the ViewCount plugin. However, much to my disappointment, Stattraq doesn’t play that nice if you use permalinks. The problem lies with the fact that Stattraq in its current version does not properly put the article id in its database if permalinks are enabled: it will use the id 0. Of course, ViewCount relies on the article id to work its magic hence the problem…
To solve this issue, I decided to improve how Stattraq was handling the article id and you can too! It’s fairly easy: open stattraq.php (it’s found in the wp-content/plugins directory of your WordPress root directory) in a text editor. Find the line that reads:
- $article_id = 0; // default/mixed page – not just for one article
and replace it by the following:
- //$article_id = 0; // default/mixed page – not just for one article
- if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])){
- $article_id = $wpdb->get_var(“SELECT ID FROM $tableposts WHERE 1=1″.$where);
- } else {
- $article_id = 0;
- }
The trick here is to use the url parsing magic found in wp-blog-header.php. That’s where the $where variable gets built. We then use it to retrieve the post id. The check on the server query is required because the index page needs to be processed differently. If the requested page is the index page, the query returns all the posts and get_var returns the id of the first record returned (which is your weblog’s first entry) which is not what we want.
Well, apparently this does not work though I am at a loss right now to figure out why. The code above, executed in the context of index.php, seems to return what I want. However, when executed in stattraq.php, it doesn’t work, resulting in the article_id field being empty… I guess I jumped the gun, thinking that I could use $where in Stattraq but it seems like it’s not going to happen without a fight. I’ll look at that when I get up.
Please note however that I haven’t fully tested this code so bad things might happen. Moreover, I’m both a WordPress and PHP newbie so chances are that my solution is neither elegant nor efficient. Also, smart readers will have noticed that this only addresses one side of the problem: the article id should now be correct in the Stattraq database. However, previous entries will still keep the “0″ article_id. How to fix this is left as an exercise to the reader.
Many thanks to Robert and Thomas for their help!
Obstacle 1 from the album Turn On the Bright Lights by Interpol
Related posts:
- Beware of File.createTempFile By default, File.createTempFile (in Java, of course) creates a temporary...
Related posts brought to you by Yet Another Related Posts Plugin.