lesterchan / wp-postviews

Enables you to display how many times a post/page had been viewed.
https://wordpress.org/plugins/wp-postviews/
114 stars 42 forks source link

Post views by Day/Week/Month #21

Closed 84pixels closed 9 years ago

84pixels commented 9 years ago

Hello,

I use your extension long time. But I would like to extend it .

I want show post by views with interval of time day/week/month.

I have found ressource like this (http://wordpress.org/extend/plugins/wordpress-popular-posts) :

function wpp_get_views($id = NULL, $range = NULL, $number_format = true) {

    // have we got an id?
    if ( empty($id) || is_null($id) || !is_numeric($id) ) {
        return "-1";
    } else {
        global $wpdb;

        $table_name = $wpdb->prefix . "popularposts";

        if ( !$range || 'all' == $range ) {
            $query = "SELECT pageviews FROM {$table_name}data WHERE postid = '{$id}'";
        } else {
            $interval = "";

            switch( $range ){
                case "yesterday":
                    $interval = "1 DAY";
                break;

                case "daily":
                    $interval = "1 DAY";
                break;

                case "weekly":
                    $interval = "1 WEEK";
                break;

                case "monthly":
                    $interval = "1 MONTH";
                break;

                default:
                    $interval = "1 DAY";
                break;
            }

            $now = current_time('mysql');

            $query = "SELECT SUM(pageviews) FROM {$table_name}summary WHERE postid = '{$id}' AND last_viewed > DATE_SUB('{$now}', INTERVAL {$interval}) LIMIT 1;";
        }

        $result = $wpdb->get_var($query);

        if ( !$result ) {
            return "0";
        }

        return ($number_format) ? number_format_i18n( intval($result) ) : $result;
    }

}

Someone can help me?

lesterchan commented 9 years ago

Sorry that is not possible. The plugin only stored accumulated views. It doesn't know each day you get how many hits. This is by design and I have no intention of changing it.

84pixels commented 9 years ago

I have made function run with our extention.

Shorcode :

top_views(array(
'range' => 'week',
'limit' => '5'
))

Function :

function top_views($args) {

    global $wpdb;
    $where = "";    
    $prefix = $wpdb->prefix;    
    $now = current_time('mysql');

    switch($args['range']){
        case "today":
            $where .= " AND p.post_date > DATE_SUB('{$now}', INTERVAL 1 DAY) ";
        break;

        case "week":
            $where .= " AND p.post_date > DATE_SUB('{$now}', INTERVAL 1 WEEK) ";
        break;

        case "month":
            $where .= " AND p.post_date > DATE_SUB('{$now}', INTERVAL 1 MONTH) ";
        break;   

        default:
            $where .= "";
        break;
    }

    $limit = "LIMIT {$args['limit']}";      

    $query = "SELECT * FROM {$wpdb->posts} AS p,{$wpdb->postmeta} AS pm WHERE pm.meta_key = 'views' AND p.ID = pm.post_id {$where}ORDER BY pm.meta_value * 1 DESC {$limit}";

    $trans_prefix = $args['range'] . '_' . $args['limit'] . 'top_10';

    if ( false === ( $results = get_transient($trans_prefix) ) ) {
            $results = $wpdb->get_results($query);
            set_transient($trans_prefix, $results, 12 * HOUR_IN_SECONDS );
    }

    return $results;

}
lesterchan commented 9 years ago

That will only show views of posts that is posted within that time range and not the views that is accumulated within the time range.