ArticlesForumDownload AboutContact

boakes.org

nice of you to drop by. tea?

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
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.

The MostWanted plugin in use.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

  1. Download this fileDownload this file
  2. Rename it mostwanted.php and copy it to your /wp-content/plugins folder.
  3. 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:

  1. $top_n
    the number of results to list default = 5
  2. $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
  3. $showviews
    true if the number of times each post has been viewed should be included in the list.
    default = false
  4. $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
  5. $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)
  6. $pre
    this is a text string that is added to every line.
    default = <li>
  7. $post
    this is a text string that is added to every line.
    default = </li>
  8. $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
  9. $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
  10. $timeout
    adjusts how long the most-wanted information is cached for before being replenished from the db. A value of 0 (zero) will result in fresh data being retrieved for every query, whereas a value of 3600 will 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:

207 Responses to “MostWanted - a Popular Posts Plugin for WordPress”

Pages: «123456789101121»

  1. 51
    Rodney Shupe Says:

    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

  2. 52
    rich boakes Says:

    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

  3. 53
    Rodney Shupe Says:

    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” );

  4. 54
    rich boakes Says:

    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:

    1. accept white and black-lists for multiple categories, enabling the blog owner to focus attenion on certain sections of the site.
    2. optionally handle hierarchical categories, so for example, if one were to select the general category on this blog, I’d like to be able top optionally include or exclude ‘n’ sub-categories.
  5. 55
    Rodney Shupe Says:

    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.

  6. 56
    matthew Says:

    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?

  7. 57
    rich boakes Says:

    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.

  8. 58
    matthew Says:

    yeah that was it, there was a space at the end of the file. All better now!!!1

    thanks for the great plugin :)

  9. 59
    matthew Says:

    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?

  10. 60
    rich boakes Says:

    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.

Pages: «123456789101121»