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
finally the same prob Chris Frazier has described.
at first I didn´t get the plugin to work at all because of the browser-issue. The whole sourcecode turned up to be shown on the plugins-page. i fixed that.
then i ran into the header-issue. fixed that.
afterwards i ran fixstats.
now i got the “cannot be called statically”-prob. i tried to understand the php5-manual-workaround but didn´t manage to.
as php5 should not be that exotic in the wild i´d have thought that Rich would already have presented a solution… Sadly i was wrong.
I think I’m flattered that you expect so much of me
(but please note that I’m not a software development company, I’m just a bloke that is making the things he writes for himself available to others free of charge).
I don’t have a server with PHP5 and I don’t have much free time, but I will be looking into it. If I can work out a fix I will publish it, if anyone else works it out, please submit it and I’ll make it available.
thanx for replying at the speed of light.
looking into the php5-manual it seems that this could be just a tiny thing to be changed. i would if i could, but unfortunately…..
and besides: happy new year!
Hi, I’d like to use your plugin and it basically works. But as soon as I activate it, the special German characters ä ö ü etc get gobbled up in the entire blog! Any clue on that?
I downloaded the PHP file, renamed it, installed it on my WordPress setup and under the plugins section it displays the source of the file instead of the actual plugin listing! I opened up the file and was suprised to find a bunch of HTML tags mixed with php, is this correct? I tried downloading with my browser and with CURL and WGET and they all returned the same file. So what am I doing wrong? Thanks,
–
Matt
Hi Rich,
You need to provide a link to get a non-HTMLised version of that file, otherwise it’s a real nightmare to get a working version!
cheers,
Chris
Hi Rich: i just activated the plugin and I get the following message in my sidebar:
WordPress database error: [Table 'studyabroad.st' doesn't exist]
SELECT p.post_title, st.article_id, COUNT( DISTINCT (st.ip_address) ) as cnt FROM st, wp_posts p where p.ID=st.article_id AND p.post_status=’publish’ AND st.user_agent_type=’0′ GROUP BY st.article_id ORDER BY cnt DESC LIMIT 0,10
Any Ideas? This is a wordpress install running on Windows IIS if that matters?
You may see it in the sidebar here: http://www.cadc.auburn.edu/soa/studyabroad/
I will keep it up for a bit in case you get time to check it out – thanks in advance.
I get the same as this
I downloaded the PHP file, renamed it, installed it on my WordPress setup and under the plugins section it displays the source of the file instead of the actual plugin listing! I opened up the file and was suprised to find a bunch of HTML tags mixed with php, is this correct? I tried downloading with my browser and with CURL and WGET and they all returned the same file. So what am I doing wrong? Thanks,
yup, just installed it, all i get is the source code filling the page…
i reckon the authors web server is html-ising all the .phps files. so none of them will work for anybody, just tried downloading an earlier version and got the same problem…
Argh! Thanks for spotting that Ben. It seems someone’s installed a filter at a lower level then wordpress and it’s automatically html-izing the php script on the way through. I’m chaining the main download to a ZIP file now.
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?)
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.
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 );}I don’t see the number of view in brackets,
I have wordpress 2.01,et mostwnated 1.8
Thanks for help
pascal
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.
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
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.
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“.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?
Rich,
I created an enhancement so that this plugin works with the new Widgets plugin.
To do this I added functions to the class. One produces the output for the Widget, One creates a control interface to set parameters, and the last initializes it. This last one is called using the plugins_loaded hook as it must run after the Widgits plugin is loaded.
Hi Rok, I think what’s wrong is that you do not have stattraq installed.
Sorry, but your plugin is incompatible with polyglot plugin and, maybe, some other filtering plugins.
Please, change code in line 243 from
$short = MostWanted::curtail($line->post_title, $curtail);
to
$short = MostWanted::curtail(apply_filters(‘the_title’, $line->post_title), $curtail) ;
This little fix make plugin compatible with any filters for WordPress.
Ahh, a widget version would be wonderful!
I did try and look at converting it myself, but my PHP foo was insufficient and so I ran away screaming..
Chris
Homo-Adminus: thanks for the tip – it’s been rolled into the 0.1.10 release which also includes Rodney Shupe’s Widget code. Released soon… once it’s been tested by a few specially WordPress trained Flying Ninja Monkeys.
Thanks Chris!
Dim problem boyo!
Pob hywl..
still wandering if it’s possible for stattraq to record and show stats for pages and not only for posts
someone know about this one?
andrezero – as far as I know it already does. I use pages on my site for my plugin documentation and they get hit often and are tracked in Stattraq – though I am still using 1.0a as I had trouble with 1.0b.
Aha – instead of calling MostWanted::mostwanted(); this seems to (mostly) work:
the odd thing I get now is that although the widget is configured to only show the top 5 I actually get the top 5 repeated twice..
Go figure!
cheers, Chris
Ok, I have taken a look at the mostwanted plugin code, since a site i am managing for one of our customers uses the plugin and I see a big load on the database caused by this plugin.
Now, I see some form of caching is implemented, but i don’t see how this caching carries over to different processes. Is the $cached_result stored in the database somewhere? I’m probably gonna patch this thing up myself to do proper caching, but of course not if it has already been solved.
I get this:
WordPress database error: [Table '******.st' doesn't exist]
SELECT COUNT( * ) as cnt FROM st where st.user_agent_type=’0′
Warning: Division by zero in …
Hi Lauren, initially this looks like a “no stattraq” problem. Is stattraq installed? Also, which version of wordpress are you using?
I installed StatTraq, but there’s an error with that, my comment on StatTraq’s site isn’t even approved yet, though. That error said that I have no auth.php in my admin folder.
I HATE 2.0, so I use 1.5.2.
THANKS!
Aha, this may be a 1.5 compatibility issue.
BTW If it’s just the 2.0 editor you don’t like, it can be turned off, permanently – I did so and 2.x is great
Actually, when it’s turned off, uploading images is still horrible. I do have the plugin Iimage Browser for some blogs, which would work, then. And I have it for this blog. But there’s also that blue and the hiding menus every time I’m logged out. I don’t know. What do you think?
I see there’s an apparent unofficial version, I think I’ll try that for now.
I get the same error with your plugin now that I’m using that 1.5 version, and that one works.
It’s good info, which I like anyway. Thanks for letting me know about it. If I don’t use your plugin, I can deal. Today, I set up a manual/static top pages thing that includes top images. So that’s extra special, if taking some work.
Chris,
When you inialize the plugin with a “new” it calls the constructor which is named the same as the class. As a result you are calling the mostwanted() function twice.
Hi
I’ve got version 1.9 and WP 2.03 but I can’t get your script to work. Whenever I add the following to my sidebar:
I get the following error
Fatal error: Non-static method MostWanted::mostwanted() cannot be called statically in /home/connecte/public_html/wp-content/themes/ag/sidebar.php on line 45
Can you help me fix this please? StatTraq is working ok so I don’t think this is the problem
Rodney wrote:
That was is! Trivial patch and it appears correctly again.
Emailing the file to Rich now after merging his validation fix.
cheers!
Chris
I have instaled the plugin on http://www.curaj.net (see the bottom of the sidebar) but instead of showing the rating of a post it shows “???” what that measn!? Can u pls help me? I also would like to ad some dots after a post title wich is not comple, how do i do it!?
hi the plugin is generating errors. “p_1.wp_stattraq’ doesnt exist”
Hi Moneyman, that suggests to me that you might not have stattraq installed, can you begin by confirming that please?
I need help. Were do i put this?
A more tuned version might read:
Not in the header, where? IS that right?
even after using the repair tool I still see this
WordPress database error: [Table 'joeblstrn1234.wp_stattraq' doesn't exist]
SELECT p.post_title, st.article_id, COUNT( DISTINCT (st.ip_address) ) as cnt FROM wp_stattraq st, wp_posts p where p.ID=st.article_id AND p.post_status=’publish’ AND st.user_agent_type=’0′ GROUP BY st.article_id ORDER BY cnt DESC LIMIT 0,7
Hey Joe,
It’s saying it can’t find the stattraq table, and on visiting your site I don’t see any evidence of stattraq. Do you have stattraq installed?
i have it on my puter but have no idea how to upload it.. instructions seem confusing…
[time passes]
ok, I think I did it right the plugin is activated and and files uploaded but still doesnt work please email me
I am getting this when I try to configure..
Currently there are
WordPress database error: [Table 'aan3854.wp_stattraq' doesn't exist]
SELECT count(*) FROM wp_stattraq WHERE article_id = ’0′ AND url LIKE ‘/index.php?name=%’
rows in your database that could be repaired.
I have my own domain and have latest WP version installed…
DO I need statTraq for this plugin?
Thanks
install quickly
I the posts are still being answered and looked into.
After activating the plugin and placing the code in the sidebar I see 3 question marks at the end of every post title in the list just like these….���