WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Stats Plugin: Excluding Homepage from stats_get_csv results (9 posts)

  1. jjsulzbach
    Member
    Posted 3 years ago #

    I am coming in to share a hack of the WordPress Stats plugin stats_get_csv() function that is used to provide statistics for several things, including a "Most Popular Posts" list using the data the plugin collects. What I am going to post here is a simple wrapper function I wrote to serve up returns from the stats_get_csv function which exclude the homepage. I have noticed in several places that there is a desire to use the Stats plugin to return a Most Popular Posts list that focuses only on blog posts, so maybe I can help someone, which I am motivated to do since I have taken more than a couple of helpful hints from others at this forum.

    I was asked to prepare the code for a "Most Read Posts" list that would display the top five most visited posts over the previous week, or previous 7 days. The website in question is a news site that uses the Gabfire Transcript blog theme, and the relevant template file where the list would be presented at the right of the page is SidebarTop.php.

    I found the documentation for the WordPress Stats plugin and noticed that on the Stats Plugin FAQs page at:

    http://wordpress.org/extend/plugins/stats/faq/

    they included code used to return a Most Popular Posts list, which I borrowed as an example and entered the following code into the SidebarTop.php template file to create the desired list (Note: this is NOT my final code):

    <div id="divMostReadPosts">
    <?php if ( function_exists('stats_get_csv') && $top_posts = stats_get_csv('postviews', 'days=7&limit=5') ) : ?>
        <h3>Most Read Recent Posts</h3>
        <ul>
    <?php foreach ( $top_posts as $p ) : ?>
            <li><a href="<?php echo $p['post_permalink']; ?>"><?php echo $p['post_title']; ?></a></li>
    <?php endforeach; ?>
        </ul>
    <?php endif; ?>
    </div>

    The code worked on the first try, but the problem it created for me is that the first page returned was the site's "Home page" which was not desirable to the site owner, since it was not a true "post." I then went looking for additional documentation to see if there might be some type of "exclude" parameter available and I found nothing, though there is one thread here on the WordPress forums that offers up an example of how to insert code into the stats_get_csv function to ignore the homepage when returning results at:

    http://wordpress.org/support/topic/plugin-wordpresscom-stats-ignore-visits-to-page-or-post?replies=1

    which I almost used, but the site owner told me he might have a need for the Stats plugin statistics on homepage visits, so that option became unattractive.

    I then set about creating a wrapper function that would handle the call to stats_get_csv() from calling code and then return the desired number of results (five in my case) going back over the desired number of days (seven, or the previous week) but which would NOT include the homepage. And what I did was to do was to write a function that accepts two parameters for the two just-described settings (desired number of days and desired number of results), but which would call stats_get_csv asking for one additional result. Afterwards the function examines those results to determine whether the homepage is included. If it is, then it removes it before returning the results to the calling code. If it is not included, then the results are examined to determine whether they were greater in number than the desired number requested by the calling code and, if yes, then the final result item is removed before returning the results.

    I'm going to post the wrapper function here in its entirety and then present an explanation of what is going on, just to help anyone who may not see it, though I actually think this is rather simple.

    <?php function get_stats_exclude_home($numDays,$numPosts) {
          $numPostsPlus = $numPosts + 1;
          $blnHasHome = false;
          $intRowsLoop = 0;
          $intRowFound;
          $intRowsCount;
          $most_read_posts;
    
          $strArgs = 'days=' . $numDays . '&limit=' . $numPostsPlus;
    
          if (function_exists('stats_get_csv')) {
            $most_read_posts = stats_get_csv('postviews', $strArgs);
          }
    
          foreach ($most_read_posts as $single_post) {
            $postTitle = $single_post['post_title'];
            if($postTitle == 'Home page') {
              $blnHasHome = true;
              $intRowFound = $intRowsLoop;
            }
            $intRowsLoop = $intRowsLoop + 1;
          }
    
          $intRowsCount = $intRowsLoop;
    
          if($blnHasHome) {
            unset($most_read_posts[$intRowFound]);
          } else {
            if($intRowsCount > $numPosts){
              unset($most_read_posts[$numPosts]);
            }
          }
    
          return $most_read_posts;
        } ?>

    Now for a walkthrough of what happens.

    As I mentioned, the function is set up to handle two parameters passed from calling code; the number of days going back to define the scope of the search ($numDays) and the number of desired results to return ($numPosts):

    function get_stats_exclude_home($numDays,$numPosts) {

    Six variables are then created for use within the function: $numPostsPlus, which takes the parameter passed and increments it by 1 to create a "throwaway" result among the returns from get_stats_csv; $blnHasHome, a boolean variable that will be used to test whether the homepage was included among the returns and which is set to "false" before the returns are tested; $intRowsLoop, a loop counter variable that is set to the number 0 at the outset so that there is a one-to-one correspondence between the loop counter's value and the 0-based array item index of the results; $intRowFound, a "placeholder" variable that will be used to store the row index of the array row in which the homepage may be found; $intRowsCount, a variable that will be used to store the number of array items returned--but not in 0-based array counting format, rather as a true count starting with 1, to compare the number of returns against the number requested by calling code; and $most_read_posts, the variable name of the array returns.

    $numPostsPlus = $numPosts + 1;
          $blnHasHome = false;
          $intRowsLoop = 0;
          $intRowFound;
          $intRowsCount;
          $most_read_posts;

    Now; to return to the original calling code and the way in which the scope of the search and the number of returns requested are called, there was this snippet:

    $top_posts = stats_get_csv('postviews', 'days=7&limit=5')

    Since I'm not accessing a different table than postviews, I hard-coded that setting within the function in my call to stats_get_csv (which is why there is no need to set it as a parameter), and all that was really needed was to concatenate a string for the second parameter containing the "days" and "limit" values to get the desired results, but in this case I wanted one extra result than what was requested by the calling code, so the actual code for "days" and "limit" is built as a concatenated string and set to the $strArgs variable as follows:

    $strArgs = 'days=' . $numDays . '&limit=' . $numPostsPlus;

    And then the stats_get_csv function is called using the variable values now included within the argument string:

    `if (function_exists('stats_get_csv')) {
    $most_read_posts = stats_get_csv('postviews', $strArgs);
    }`

    After receiving the results from stats_get_csv, they are then checked within a for ... each loop in which each array item's post title string is compared against the known title for the homepage and, if it is found (meaning "if the homepage is found among the results"), two things are done. One; the boolean variable $blnHasHome is set to "true." And two; the placeholder variable $intRowFound is set to the current value of the loop counter variable $intRowsLoop to save for later when the row will be removed from the array.

    foreach ($most_read_posts as $single_post) {
            $postTitle = $single_post['post_title'];
            if($postTitle == 'Home page') {
              $blnHasHome = true;
              $intRowFound = $intRowsLoop;
            }
            $intRowsLoop = $intRowsLoop + 1;
          }

    After the execution of the above loop the function now knows whether the homepage was returned and what its array item index is in the event it was found.

    Just in the event that the homepage was not found, the number of items in the array containing the results from the function's call to stats_get_csv is saved to the final value of the loop counter variable, to have in the event that a row item other than the homepage must be deleted before returning the results to the calling code:

    $intRowsCount = $intRowsLoop;

    All that is left to do now before the results are returned is to execute a simple if ... else branch that first checks the value of the boolean variable $blnHasHome to see if the homepage was found and, if it was ($blnHasHome will equal "true"), to use PHP's built-in "unset" function to remove the array item at the index saved to the $intRowFound variable or, if the homepage was not found ($blnHasHome equals "false"), to use the unset function to remove the last item in the array, but only after testing the total number of items returned to make certain that they are greater than the number requested, which is the value of the $numPosts parameter sent from the calling code:

    if($blnHasHome) {
            unset($most_read_posts[$intRowFound]);
          } else {
            if($intRowsCount > $numPosts){
              unset($most_read_posts[$numPosts]);
            }
          }
    }

    Then the array is returned to the calling code, without any homepage included:

    return $most_read_posts;

    I saved the function into the same SidebarTop.php template file where the list is called for and the code which calls it (compare with my original code at the top of this post), looks like this:

    <div id="divMostReadPosts">
    <?php if ( function_exists('get_stats_exclude_home') && $top_posts = get_stats_exclude_home(7,5) ) : ?>
        <h3>Most Read Recent Posts</h3>
        <ul>
    <?php foreach ( $top_posts as $p ) : ?>
            <li><a href="<?php echo $p['post_permalink']; ?>"><?php echo $p['post_title']; ?></a></li>
    <?php endforeach; ?>
        </ul>
    <?php endif; ?>
    </div>

    No single quotes are required for the parameters "7,5" because the values are passed as numbers, not strings.

    One final note; I did show the results of a second type of display that was not preferred by the site owner to the one used just above, but which contained the number of page views shown within quotation marks, something like -- The Title of A Post (127 views) -- so I'm going to post that code as well, in case someone may need it:

    <div id="divMostReadPosts">
    <?php if ( function_exists('get_stats_exclude_home') && $top_posts = get_stats_exclude_home(7,5) ) : ?>
        <h3>Most Read Recent Posts</h3>
        <ul>
    <?php foreach ( $top_posts as $p ) : ?>
            <li><a href="<?php echo $p['post_permalink']; ?>"><?php echo $p['post_title']; ?></a> (<?php echo $p['views']; ?> views)</li>
    <?php endforeach; ?>
        </ul>
    <?php endif; ?>
    </div>

    Hope this helps somebody.

  2. jjsulzbach
    Member
    Posted 3 years ago #

    Doggone it!

    Please forgive me everyone, but I just went back and checked my original code and I have found a mistake in my above code.

    I have:

    $intRowsCount = $intRowsLoop;

    That should read:

    $intRowsCount = $intRowsLoop + 1

    Forgive me everyone, it was late.

    My bad, sorry.

  3. jjsulzbach
    Member
    Posted 3 years ago #

    Well; a friend just e-mailed me and told me I ought to post the whole function as it should be and since he's right ...

    <?php function get_stats_exclude_home($numDays,$numPosts) {
          $numPostsPlus = $numPosts + 1;
          $blnHasHome = false;
          $intRowsLoop = 0;
          $intRowFound;
          $intRowsCount;
          $most_read_posts;
    
          $strArgs = 'days=' . $numDays . '&limit=' . $numPostsPlus;
    
          if (function_exists('stats_get_csv')) {
            $most_read_posts = stats_get_csv('postviews', $strArgs);
          }
    
          foreach ($most_read_posts as $single_post) {
            $postTitle = $single_post['post_title'];
            if($postTitle == 'Home page') {
              $blnHasHome = true;
              $intRowFound = $intRowsLoop;
            }
            $intRowsLoop = $intRowsLoop + 1;
          }
    
          $intRowsCount = $intRowsLoop + 1;
    
          if($blnHasHome) {
            unset($most_read_posts[$intRowFound]);
          } else {
            if($intRowsCount > $numPosts){
              unset($most_read_posts[$numPosts]);
            }
          }
    
          return $most_read_posts;
        } ?>
  4. Klark0
    Member
    Posted 3 years ago #

    THANKS!!!

    They really should put this in the plugin. But i guess they're worrying about the load from all the extra requests to wp.com servers. So this should probably be used with a Page caching plugin and use long expire times.

  5. Klark0
    Member
    Posted 3 years ago #

    Hey,

    Any idea on how to exclude pages from the list?

  6. jjsulzbach
    Member
    Posted 3 years ago #

    Klark0,

    Yes, it only requires a short modification to the above function, and what I will post here will work for only excluding one page, as the above function does.

    Find this section of code:

    foreach ($most_read_posts as $single_post) {
            $postTitle = $single_post['post_title'];
            if($postTitle == 'Home page') {
              $blnHasHome = true;
              $intRowFound = $intRowsLoop;
            }
            $intRowsLoop = $intRowsLoop + 1;
          }

    and go to this line:

    if($postTitle == 'Home page') {

    All you have to do is replace 'Home page' with the post_title value of the page you wish to exclude.

    Keep in mind that if you want to exclude more than one page, say the homepage plus an "about me" page or something like that, that the algorithm used above would have to change, and how it would be changed would depend upon how many pages you wanted to exclude. I don't want to start speculating on that right now, just that this comment does zero in on where the naming of the page to be excluded takes place.

  7. Anthony Somerset
    Member
    Posted 3 years ago #

    excellent posting

    i put all that code in a custom php widget and it works perfectly

    any suggestions on getting post views and post date?

  8. MikeMovies
    Member
    Posted 3 years ago #

    absolute life saver. It was annoying me to no end before with that stupid Homepage showing since mine linked to stats.wordpress.com. Kudos!

  9. aparentdesign
    Member
    Posted 3 years ago #

    Thanks for this!

Topic Closed

This topic has been closed to new replies.

About this Topic