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


June 14th, 2005 at 11:56 pm
Another suggestion was to add category support I also added that to my install by first modifing the function declaration to accept an additional optional parameter ($cat_id):
function rjb_mostwanted($top_n=5, $trim_chars=0, $showviews=false, $show_views_in_tt=true, $cat_id = -1){
I then changed the query to factor in the category id passed or ignore if not passed:
$output = $wpdb->get_results( “SELECT p.post_title, st.article_id, COUNT( DISTINCT (st.ip_address) ) as cnt FROM $tablestattraq st JOIN $tableposts p ON p.ID=st.article_id LEFT JOIN $wpdb->post2cat pc ON p.ID = pc.post_id WHERE (IFNULL(pc.category_id, -1) = $cat_id OR $cat_id
June 15th, 2005 at 1:32 am
Hi Rodney,
I am delighted to confirm that permalinks are in the latest version (0.1.0), and that I’d had forgotten all about it, so I’ve updated the change log too, thanks for the prompt.
Also it looks like your useful category addition got clipped somehow. If you repost it I’ll have a go at integrating it into 0.1.1.
Rich
June 15th, 2005 at 7:06 am
Here is the query code:
$output = $wpdb->get_results( “SELECT p.post_title, st.article_id, COUNT( DISTINCT (st.ip_address) ) as cnt FROM $tablestattraq st JOIN $tableposts p ON p.ID=st.article_id LEFT JOIN $wpdb->post2cat pc ON p.ID = pc.post_id WHERE (IFNULL(pc.category_id, -1) = $cat_id OR $cat_id < 0) “.$futureSpamOption.” GROUP BY st.article_id ORDER BY cnt DESC LIMIT 0,$top_n” );
June 15th, 2005 at 8:05 am
Thanks for that Rodney,
I’ve coded something functionally similar (but less elegant) in the past and since that means there’s at least two of us who think it would be useful I’ll add it to the 0.1.1 release.
Ultimately what I’d like to see it do is:
June 15th, 2005 at 8:45 pm
Well maybe I can help with that.
You could change the function declaration like this:
function rjb_mostwanted($top_n=5, $trim_chars=0, $showviews=false, $show_views_in_tt=true, $cat_white_list=”, $cat_black_list=”){
And then change the query to the following two lines:
$cat_where = “((”.($cat_white_list!=” ? “IFNULL(pc.category_id, -1) IN ($cat_white_list) OR ” : “”).”‘$cat_white_list’ = ”)”. ($cat_black_list!=” ? ” AND p.ID NOT IN (SELECT pc2.post_id FROM $wpdb->post2cat pc2 WHERE pc2.post_id=p.ID AND pc2.category_id IN ($cat_black_list))” : “”).”)”;
$output = $wpdb->get_results( “SELECT p.post_title, st.article_id, COUNT( DISTINCT (st.ip_address) ) as cnt FROM $tablestattraq st JOIN $tableposts p ON p.ID=st.article_id LEFT JOIN $wpdb->post2cat pc ON p.ID = pc.post_id WHERE $cat_where “.$futureSpamOption.” GROUP BY st.article_id ORDER BY cnt DESC LIMIT 0,$top_n” );
The way this would work is the two new parameters would take optional comma seperated lists.
For example to return most recents in categories 5 and 3:
rjb_mostwanted(5, 0, false, true, ‘5,3′);
For example to return most recents in categories 5 and 3 but not if also in 9:
rjb_mostwanted(5, 0, false, true, ‘5,3′, ‘9′);
For example to return most recents in categories 5 and 3 but not if also in 4 or 9:
rjb_mostwanted(5, 0, false, true, ‘5,3′, ‘4,9′);
For example to return most recents in all categories with the exception of 4 or 9:
rjb_mostwanted(5, 0, false, true, ”, ‘4,9′);
Note this is backward compatable with my first solution.
June 16th, 2005 at 3:06 am
I am getting a strange error when i click on the activation button. Suddenly this appears at the top of my Dashboard
Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blog/wp-content/plugins/mostwanted_0.1.0.php:183) in /var/www/html/blog/wp-admin/admin.php on line 10
Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blog/wp-content/plugins/mostwanted_0.1.0.php:183) in /var/www/html/blog/wp-admin/admin.php on line 11
Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blog/wp-content/plugins/mostwanted_0.1.0.php:183) in /var/www/html/blog/wp-admin/admin.php on line 12
Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/blog/wp-content/plugins/mostwanted_0.1.0.php:183) in /var/www/html/blog/wp-admin/admin.php on line 13
see for yourself
http://www.offcentercrew.com/before.jpg
http://www.offcentercrew.com/after.jpg
any idea what’s going on?
June 16th, 2005 at 8:54 am
The usual reason for seeing this is if there’s been any space introduced before or after the opening and closing php statements in the plugin i.e. the very first characters in the plugin must be <?php and the very last must be ?> with no newline or space after it.
This is a quirk of wordpress/php and is the reason why I provide a phps file rather than pasting the code on this page.
I added a newline to the 0.1.0 code and was able to reproduce similar output to what you saw. If this is not what’s given you the error, then we’ll have to dig deeper.
June 16th, 2005 at 3:15 pm
yeah that was it, there was a space at the end of the file. All better now!!!1
thanks for the great plugin :)
June 17th, 2005 at 2:29 pm
sorry, 1 more slight problem
right before it lists the most viewed entries i get an error message in the side bar saying:
“Warning: Missing argument 5 for mostwanted() in /var/www/html/blog/wp-content/plugins/mostwanted_0.1.0.php on line 119″
here is what is on line 119:
117: class MostWanted {
118:
119: function mostwanted($top_n=5, $curtail=0, $showviews=false, $show_views_in_tt=true, $duration, $pre=”", $post=”", $method=”ip”){
other than this error message everything seems to work fine. I can set all the parameters like number of posts to list etc and all the links work fine, just this error message in my sidebar is kind of an eyesore
any ideas?
June 17th, 2005 at 2:51 pm
I spotted that on your blog yesterday and implemented a fix last night (emailed your yahoo account about it) MostWanted 0.1.2 will I think (hope) fix the problem.