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


March 13th, 2006 at 1:04 pm
does anyone know how to make stattraq to record stats for pages?
It aparently only records hits for blog posts, and I love to use wordpress as a CMS
I need to know if users have visited a certain page (like my resume download page, get it?)
March 15th, 2006 at 9:20 pm
Found a small bug in the plugin. If StatTraq is Deactivated you recieve a SQL query error as shown in Rusty Smith’s comment above.
This is simple to fix by adding a if block around the query call. I just added:
if(isset($tablestattraq)) {
to line 229 and closed of the if block on line 233.
March 17th, 2006 at 12:15 am
My previous post gave me an idea as to using this for other plugins not just StatTraq. I have recently started using a plugin called WP-PostViews and wanted to use the with MostWanted.
What I did was modify the query sections of both the mostwanted and getHits functions.
Here are the two code snippets:
if(isset($tablestattraq)) {$q = "SELECT p.post_title, st.article_id, COUNT( DISTINCT (st.$method) ) as cnt FROM $tablestattraq st, $wpdb->posts p WHERE p.ID=st.article_id AND p.post_status='publish' ".$futureSpamOption." AND st.user_agent_type='0' ".$dateOption." GROUP BY st.article_id ORDER BY cnt DESC LIMIT 0,$top_n";$output = $wpdb->get_results( $q );} elseif(function_exists('process_postviews')) {$q = "SELECT p.post_title, p.ID as article_id, CAST(meta_value AS UNSIGNED) AS cnt FROM $wpdb->posts p, $wpdb->postmeta m WHERE m.post_id = p.ID AND (p.post_status = 'publish' OR post_status = 'static') ".$futureSpamOption." AND meta_key = 'views' AND post_password = '' ORDER BY cnt DESC, p.post_date DESC LIMIT 0,$top_n";$output = $wpdb->get_results( $q );}And:
if(isset($tablestattraq)) {$q = "SELECT COUNT( * ) as cnt FROM wp_stattraq st where st.user_agent_type='0'" . $futureSpamOption." ".$dateOption;$total_hits = $wpdb->get_var( $q );} elseif(function_exists('process_postviews')) {$q = "SELECT COUNT(CAST(m.meta_value AS UNSIGNED)) AS cnt FROM $wpdb->postmeta m WHERE meta_key = 'views'";
$total_hits = $wpdb->get_var( $q );}March 17th, 2006 at 9:29 am
I don’t see the number of view in brackets,
I have wordpress 2.01,et mostwnated 1.8
Thanks for help
pascal
March 17th, 2006 at 10:43 am
Hi Pascal, can you paste the code that you’re using to call mostwanted - you’ll need to do remove the <?php and ?> so that it doesn’t get stripped off en-route.
March 25th, 2006 at 7:39 am
Rich,
I was wondering if there was a fix for stattraq for wordpress 2.01… Its counting all of my hit to the admin pages and stuff… also, it double counts all the other pages, once as mixed and once as the right page.
Also, I would like to enable WP-Cache, but when i do it pretty much destroys the tracking.
Any help would be excellent,
Justin
March 31st, 2006 at 12:21 pm
April 4th, 2006 at 12:24 am
Hi there,
I have downloaded and saved the file as mostwanter.php and there after activated this from the Plugin panel.
I had then placed this code `…` in sidebar.php. And I get this error
Could you please advice “what is wrong?”.
Thanks in advance.
April 4th, 2006 at 8:33 am
Hi Rok, the query you’ve pasted is the key here. Where it says
"... as cnt FROM st, wp_posts p ..."I would expect it to say"... as cnt FROMwp_stattraq st, wp_posts p …” . This suggests that the variable which contains the name of the stattraq table has not been set, which in turn suggests that stattraq has not been installed. I took a guess at what your stattraq url should be and I got a “not found” error. The answer is probably “you need to install stattraq“.April 4th, 2006 at 1:18 pm
Hi Rich,
Thanks for the reply. I have checked all is working fine. The wp-config.php has got the correct user name/passord. Host and the DB Server running.
I am confused what’s wrong?