Title: Need to tweak this function
Last modified: August 20, 2016

---

# Need to tweak this function

 *  Resolved [russface](https://wordpress.org/support/users/russface/)
 * (@russface)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/need-to-tweak-this-function/)
 * Someone else wrote this function which displays the “most viewed” on my site,
   but it doesn’t have an end date. So if an article from 2 years ago is my most
   viewed it’s still in there.
 * To remove items as the got old, I created a category called “older” and I want
   items that I put in that category to disappear from the list.
 * I tried to add something like
    `AND category <> 'older'` or `AND post_category
   <> 'older'`
 * but that doesn’t work. Can you help? Thanks!
 * Original code (from functions.php) below…
 *     ```
       function wow_get_most_viewed($limit = 5) {
               global $wpdb;
   
               $where = "post_type = 'post'";
               $output = '';
   
               $most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/need-to-tweak-this-function/#post-2988207)
 * You could set a limit on the number of months back to allow with this:
 *     ```
       $where = "post_type = 'post'";
       $output = '';
       $months_back = 13; // The number of months back to allow most viewed
       $current_date = current_time('mysql');
       $date_back = date('Y-m-d',strtotime("$current_date - $months_back months"));
   
        $most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date BETWEEN '$date_back' AND '$current_date' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
       ```
   
 * Please note that I have not tested this, so watch for typos.
 *  Thread Starter [russface](https://wordpress.org/support/users/russface/)
 * (@russface)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/need-to-tweak-this-function/#post-2988210)
 * That works too! Thanks a ton.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/need-to-tweak-this-function/#post-2988211)
 * If your problem has been solved, please use the dropdown on the right to mark
   this topic ‘Resolved’ so that anyone else with this question can see that there
   is a solution.
 *  Thread Starter [russface](https://wordpress.org/support/users/russface/)
 * (@russface)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/need-to-tweak-this-function/#post-2988212)
 * Thanks

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Need to tweak this function’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [russface](https://wordpress.org/support/users/russface/)
 * Last activity: [13 years, 7 months ago](https://wordpress.org/support/topic/need-to-tweak-this-function/#post-2988212)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
