Tags: Releases, WordPress
MostWanted - a Popular Posts Plugin for WordPress
November 24th, 2004, by Rich.
Warning: apache_lookup_uri() [function.apache-lookup-uri]: Unable to include '/pics/2005/mostwanted/mostwanted' - error finding URI in /home/www/boakes.org/htdocs/mods/plugins/boakes-depicticon.php on line 65
This WordPress Plugin which lists the most popular posts in a blog, according to the records held by StatTraq…
MostWanted lists the most popular posts on a wordpress powered weblog. This list can be used in the sidebar to provided visitors with an indication of what are the most visited pages.
It’s grown a little since it’s original release so it has a subtle API change. Where previously it was called “rjb_mostwanted”, it is now “MostWanted::mostwanted”. Currently there is a wrapper around the old method name so that it still works, however this will be removed in future releases.
Installation
Download this file- Rename it mostwanted.php and copy it to your
/wp-content/pluginsfolder. - using wp-admin, enable the plugin
Usage
The plugin provides one method of interest: MostWanted::mostwanted($top_n, $trim_chars, $showviews). The three parameters are:
$top_n
the number of results to list default = 5$curtail
0 for no text curtailment, or ‘n’ the number of characters from each post title that shoudl be displayed. e.g. Curtailing “My Dynamic Badger” to 10 characters would read “My Dyna…”.
default = no curtailment$showviews
true if the number of times each post has been viewed should be included in the list.
default = false$show_views_in_tt
TT is short for ToolTip setting this value to true includes the number of views as part of the tool tip. i.e. if you hover over the text the (1234 distinct viewers) message is shown.
default = true$duration
restricts the duration of the query period so that only the last $duration days are considered when measuring popularity. e.g. a value of 30 would return the number of users only within the last 30 days. Leaving the value unset, or 0, results in the all-time results being returned.
default = 0 (all-time)$pre
this is a text string that is added to every line.
default = <li>$post
this is a text string that is added to every line.
default = </li>$method
there are two ways of identifying unique visitors, neither of which are perfect, the default is to recognize only unique IP addresses, which means that if several people from one company visit, then they may show as a single user. Alternatively, using the session_id is not perfect because some users refuse to set cookies.
default = ip, alternative = session$as_percentage
if set to true, then the number of hits for each page as a percentage of the site total is displayed instead of the hit count itself. This may be desirable if you want to show popularity without letting on how many hits you get for each story.
default = false$timeout
adjusts how long the most-wanted information is cached for before being replenished from the db. A value of0(zero) will result in fresh data being retrieved for every query, whereas a value of3600will only query the database once every hour, cacheing the output and thus reducing DB load.
default = 1800
The simplest way to use the plugin is therefore to augment your page with:
<ul>
<?php MostWanted::mostwanted(); ?>
</ul>
A more tuned version might read:
<ul>
<?php MostWanted::mostwanted(7, 30, true); ?>
</ul>
License
MostWanted is released under a Creative Commons License.
Credits
If you find MostWanted useful, please feel free to link or a trackback to this entry.
Thanks to everyone whose commented with problems, solutions & suggestions, especially:
- Ben Gracewood whose previous suggestion here gave me enough of a head start that I was able to come up with the relevant SQL query.
- Randy Peterman
- Darryll Van Dorp
- Michelle Li
- Rodney Shupe
- Mike Smith


May 5th, 2005 at 4:28 pm
Thanks Steve, I think the problem may be that you’re using permalinks, and specifically, that you were using permalinks since before stattraq started recording data. The result is that there are no records in the database with a post ID; and consequently we see the error.
[if that's the case, thanks for helping me track it down!]
I’ve just made a 0.0.7 build available which:
To use fixstats, you need to download this file and save it in your wp-stattraq directory as “fixstats.php”. To run it, open stattraq as normal then add “fixstats.php” to the URL. Depending on the volume of stats you have recorded it should take between zero and several minutes to complete.
Hopefully, once this is run, the error will go away.
But that’s not all. You need to tweak the stattraq code a little in order to avoid having to do this again (Randy has promised this will be solved in the next release of StatTraq).
open up stattraq.php and look for
[php]if (($p != ”)){
$p = intval($p);[/php]
Which will be around line 25. Replace the above two lines with
if (($post->ID != ”)){
$p = intval($post->ID);
and your MostWanted stats will forever remain up to date.
I hope.
Let me know how you get on.
May 5th, 2005 at 4:54 pm
Thanks for helping out. Am I to understand the plug-in won’t work with permalinks at all? Or is this fixstats meant to correct for that?
Anyway, two problems that keep me from proceeding. Your link to “fixstats.php” is broken and I get this site-hosing error from 0.0.07:
Parse error: parse error, unexpected ‘{’ in /srv/www/arlo/automatorworld/htdocs/wordpress/wp-content/plugins/mostwanted.php on line 69
May 5th, 2005 at 5:27 pm
MostWanted works with the stattraq stats, and it’s a curiosity of stattraq that if permalinks are used then it doesn’t record the post ID, so without the tweak outlined above, permalink based blogs won’t work with MostWanted (until StatTraq is updated - this is something that Randy is planning on fixing in the next release). In the mean time I’m using using permalinks on this site, with MostWanted, and it all does work, providing you change the 2 lines specified above :).
re: your 2 problems - sorry, both fixed I hope - try again!
May 5th, 2005 at 8:28 pm
Rich-
So far, so good–I think. It’s been running about an hour now, but stattraq hasn’t updated with specific post hits yet..and thus mostwanted is showing “no results available”. How long should it take for new posts to filter into the db? I’ve had about 1,000 page views in the last hour.
May 5th, 2005 at 10:01 pm
I ran fixstats.php, and after fixing the require path to “../wp-config.php” ;) I got this error:
Fatal error: Class ‘SpamTraq’ not found in /wp-stattraq/fixstats.php on line 4
May 5th, 2005 at 10:03 pm
hmmm. thinking around the possibilities, is stattraq in the “standard” directory & tables?
May 5th, 2005 at 10:15 pm
As a goof I went into the sql and changed an entry ID from 0 to a real number, and sure enough it appeared properly. So for whatever reason the change to statrraq.php is going ignored. Sorry for 3 messages in a row…
May 6th, 2005 at 1:09 pm
re: “Fatal error: Class ‘SpamTraq’ not found…” that’s my bad. Either remove “SpamTraq::” from the file or re-download it, I’ve replaced it at this end.
May 6th, 2005 at 1:34 pm
Rich -
ok, fixstats worked great! Still one problem: the hack to stattraq isn’t working; new post hits aren’t recording the ID.
May 6th, 2005 at 1:47 pm
For clarity, here’s what I have in my stattraq.php from lines 25 to 37:
// need to get the real article_id or type of server request (RSS, RDF, ATOM, Ping, etc)
if(!isset($article_id)){
//if (($p != '')){
// $p = intval($p);
if (($post->ID != '')){
$p = intval($post->ID);
$article_id = $p;
}else if(isset($doing_rss) && $doing_rss == 1){
$article_id = “Feed”;
}else{
$article_id = 0; // default/mixed page - not just for one article
}
}
I’m wondering if the single quotes may have been munged by my server and hence it would have stopped working.