• Resolved russface

    (@russface)


    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)
  • 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

    (@russface)

    That works too! Thanks a ton.

    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

    (@russface)

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Need to tweak this function’ is closed to new replies.