MostWanted – a Popular Posts Plugin for WordPress
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
221 Comments
I just installed it and started clicking links to load up the stats. I can see the link hits but the first 2 most popular are not listing.
response: Ah! this is a hangover from an earlier test version. installed plugins should change the sql statment from reading “DESC LIMIT 2,$top_n” to “DESC LIMIT 0,$top_n”. I’ve updated the copy on this page.
Also “Reader Dave’s Double Wall” gets a slash added to “Reader Dave/’s Double Wall.”
response:Looks like an escaping issue – will investigate.
With: <?php rjb_mostwanted(10, 30, true); ?>
i receive the error:
Warning: Invalid argument supplied for foreach() in /home/html/wordpress/wp-content/ plugins/MostWanted.php on line 54can you help me please?
Thanks!
response: investigating…
update: cannot reproduce – need more info – are you using url rewriting?
I am getting the same error as comment number 4!!
I have WP installed 2 times on my site, in the same DB but with different tablenames, as well as stattraq plugin on both.
MostWanted plugin works, but not right. On one blog it shows what is correct, on the other it shows the wrong titles, right links though. The wrong titles are from my other blog …
Any clue? (I don’t have this on my blog, but on a separate php file not linked)
rich: possibly (hopefully) fixed by the 0.0.4 release.
Also, I did have to put a "." at line 60 before the slash to make the links go to the right folder WP is installed in for each.
such as:
"./index.php?p="Would that be messing it up?rich: That would explain the links being “right” but the titles being wrong.
The error referred to in post #4 seems to come from SEO use (mod_rewrite that is). I’ll look into the code to see if there’s something to be done. This looked promissing.
IS this thing on?
response: yes it is. and in answer to your other (repeated) question, my email address should be pretty simple to guess since you know the domain name already…
I’m getting a database error (table doesn’t exist) on my main page. I’m using the function in a php file in the public_html root directory rather than the /blog directory my blog is in. Am I doing something wrong?
rich: hmmm. i just took a look at your site and i can see that stattraq is working normally, so initially i’m stumped… i’ll have a root around. is everything else normal? are you running this in a page where you’ve already done:
require('wp-blog-header.php');I’m getting the same error as post #4
Fresh install of Apache 2 on XP professional.
Wordpress 1.22
don’t know what I’m doing much more than that!
Warning: Invalid argument supplied for foreach() in yadayada…\wp-content\plugins\mostwanted.php on line 57
Hey, trying out your plugin. Is there a place to get the code without cutting and pasting ? Right now I have to go in an manuall erase all the line numbers….
Thanks for the suggestion Darryl, there’s a quirk between IE and Firefox that I’d not noticed – IE copies only the text, whereas Firefox copies the line numbers too. I’ve added a downloadable version as requested.
Thanks!
I’m getting the invalid argument error as well, WP 1.5. Any fix for this?
Another question– is there any way to include or exclude categories? For example, I would want toshow just the top 5 hits from posts in category X and y..
Hi Steve, Which template page are you adding it to, on which theme?
Kubrick was the starting theme, but its way past resembling it anymore
The code is in my sidebar. See my email addy for the site.
I’ve just updated the plugin to 0.0.6. The changes are unlikely to affect the error, but if you can update your version then at lest your errors and my version number will be consistent.
FWIW The 0.0.6 update isn’t particularly impotant and doesn’t need upgrading to unless anyone else has a problem. It’s a formalization of the things I’ve been doing to stattraq to combat referral spam.
Same error:
Warning: Invalid argument supplied for foreach() in /wordpress/wp-content/plugins/mostwanted.php on line 65
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.
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
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!
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.
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
hmmm. thinking around the possibilities, is stattraq in the “standard” directory & tables?
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…
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.
Rich -
ok, fixstats worked great! Still one problem: the hack to stattraq isn’t working; new post hits aren’t recording the ID.
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.
I swapped out that portion, but still no post ID’s..everything is set as “0″. Maybe send me your whole stattraq.php file? Maybe you changed something else and forgot about it?
A few additional things:
- Although it is updating newer entries, when I run fixstats, it produces the identical list of 64 fixed entries every time.
- Some RSS feed hits are being ignored (set to ’0′). I guess this is a problem for Randy, but couldn’t hurt to mention it here (and he seems to be MIA for a month now).
- I found a bug in your plugin, line 75: “‘ href=’/index-normal.php?p=” I changed that back to ‘index.php’
Requests:
- Can you add a parameter to list only the posts of given category(s)? For example,
php rjb_mostwanted(10, 30, true, 1,2);would list only the top 10 posts in categories 1 and 2.- Would it be possible to change the above line 75 to use the permalink, if the WP install is using permalinks, instead of post ID?
Thanks..
Steve
I’m getting an error that says maximum execution time of 30 seconds has been exceeded when running the fixstats function.
What setting do I need to change?
found my answer… php.ini!
I have one problem: my listing (mostwanted pages on sidebar) shows correct URLs for articles, but incorrect URLs for pages.
Pages have link syntax: http://www.yoursite.com/?page_id=number
and your plugin shows http://www.yoursite.com/?p=number (wrong, this is syntax for articles, not for pages).
Any help or idea?
Many thanks, Martin
I have one problem: my listing (mostwanted pages on sidebar) shows correct URLs for articles, but incorrect URLs for pages.
Pages have link syntax: /?page_id=number
and your plugin shows /?p=number (wrong, this is syntax for articles, not for pages).
Any help or idea?
Many thanks, Martin
My Popular shows for ever: No results available.
But stattraq is still logging and works fine.
Wordpress 1.5, any idea?
Thanks
Ops I insert the wrog url the blog where this plugin doesn’t work is http://www.gotroot.it
Update: I’ve done a clean install of WP 1.5.1, on a completely different server, and I’m still having the problem of stattraq not recording permalinks, even with your modified code–I’m running fixstats.php every day manually
I’ve cross-posted over at stattraq site in hopes to solve this..I really need it to work.
Hi I am using WordPress 1.5 and hacked a quick solution to fix permalink storage in the database. My StatTraq now works for all permalinks. This is also specially set for permalinks using month, day, and year. Sorry if the naming is bad. I find the
post_namein the database and check against the timestamp. You can adjust it if you use other formating. I dunno why but I can’t post on the StatTraq website. Please let me know how it goes. =)instructions:
right under
$article_id = 0; // default/mixed page - not just for one article
place the following:
// BEGIN MICHELLE CODE
$myName = $_GET['name'];
$myMon = intval($_GET['monthnum']);
$myYear = $_GET['year'];
$myDay = $_GET['day'];
$mysqlQuery = "SELECT ID as pid, UNIX_TIMESTAMP(post_date) as pdate FROM wp_posts WHERE post_name='$myName'";
$myOutput = $wpdb->get_results($mysqlQuery);
if (isset($myOutput)) {
foreach ($myOutput as $myline) {
$sDate = getdate($myline->pdate);
$myMon2 = $sDate['mon'];
$myYear2 = $sDate['year'];
$myDay2 = $sDate['mday'];
if($myMon==$myMon2 && $myYear==$myYear2 && $myDay==$myDay2) {
$article_id = $myline->pid;
}
}
}
// END MICHELLE CODE
MICHELLE YOU GODDESS!!! This works PEREFECTLY!
Thankyouthankyouthankyou.
When I activate mostwanted I get this error in WP 1.5.1
Warning: Cannot modify header information – headers already sent by (output started at /hsphere/local/home/dhthomas/glitteringstew.com/muse/wp-content/plugins/rjb_mostwanted.php:116) in /hsphere/local/home/dhthomas/glitteringstew.com/muse/wp-admin/admin.php on line 10
But the mostwanted info. at least appears in the sidebar, though with no stats because I just installed it.
David
Hey, I tried running the fixstat script and I get this error
Fatal error: Call to undefined function: fixstats() in /hsphere/local/home/dhthomas/glitteringstew.com/muse/wp-stattraq/fixstats.php on line 4
I placed it in the wp-stattraq folder.
Also, when I activate the mostwanted plugin in wp-admin I get this error
Warning: Cannot modify header information – headers already sent by (output started at /hsphere/local/home/dhthomas/glitteringstew.com/muse/wp-content/plugins/rjb_mostwanted.php:116) in /hsphere/local/home/dhthomas/glitteringstew.com/muse/wp-admin/admin.php on line 10
Please advise. I’m not sure what is going wrong.
David
I managed to get the statfix to run, but the links to the MostWanted list of posts still uses the post number, with a strange path, index.php?p=77, not the permalink. (I changed the index.php-normal to index.php, but no change in the link effectiveness)
I changed the code for post ID as suggested by Rich above, I even added the snippet of code suggested by Michelle, since I use the date in the permalink.
Any suggestions? Maybe I just need to wait for the new release of stattraq.
David
Sorry for so many comments.
Infinite apologies, especially to rich. I got it. my blog is in a subdirectory, so I had to change the path from /index.php to /sub/index.php
All is well. please ignore those messages above.
Thank you for a fine plugin of a plugin. Now someone should invent a plugin for your plugin of a plugin.
cheers,
D
I still can’t get the plugin to play nice with the admin headers. The plugin works on my blog, but shuts down admin, and the error code above is what I get, and later, a similar warning about wp-includes/plugable.php malfunction, again with headers.
I really like this plugin and I hope I can figure out what is not clicking. Thanks for any input. David
Hey, you might want to consider letting the mostwanted plugin filter out bots.
$output = $wpdb->get_results( “SELECT p.post_title, st.article_id, COUNT( DISTINCT (st.session_id) ) as cnt FROM $tablestattraq st,
$tableposts p where p.ID=st.article_id and st.user_agent_type=’0′ GROUP BY st.article_id ORDER BY cnt DESC LIMIT 0,$top_n” );
The key point being user_agent_type = ’0′
Also as an idea it’s a quick and easy change to your function to limit the results to ‘x’ days (which i like
$output = $wpdb->get_results( “SELECT p.post_title, st.article_id, COUNT( DISTINCT (st.session_id) ) as cnt FROM $tablestattraq st, $tableposts p where p.ID=st.article_id AND st.user_agent_type=’0′ AND DATE_SUB(CURDATE(),INTERVAL $interval DAY) < st.access_time GROUP BY st.article_id ORDER BY cnt DESC LIMIT 0,$top_n†)
Thanks for the suggestions Darryl, I’ll incorporate both in a 0.0.8 release.
I hope you implement the sugestion above to use get_permalink on line 75 I implemented it on my install and it works great:
"’ href=’" . get_permalink($line->article_id) . "’>"
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
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
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” );
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: