Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #18911
    NowbugLeBrave
    Participant

    Hi,

    First thank you for this plugin, really excellent!

    I would like to know if it is possible to retrieve the list of posts by views, according to the interval between two dates?

    Eg: retrieve the list of views there are 15 days (or month). CURRENT DATE – 15 DAYS

    Thx!

    #21031
    Riddler
    Participant

    I was searching the same, but couldn’t find anything, so i wrote my own “quick and dirty” function. Maybe someone find it useful. The function accepts three parameters:

    • $post_type = Name of a post type
    • $days = Number of days that should be included up to the current date
    • $limit = The number of posts

    It’s a very specific function and not as comfortable as a WP_Query request, but i thought i share this here :)

    function pvc_get_posts_by_interval( string $post_type, int $days = 3, int $limit = 6 ) 
    {
    	global $wpdb;
    
    	$date_since = date( 'Ymd', strtotime( "-{$days} days" ) );
    
    	$query = "SELECT wpp.*, SUM(pvc.count) AS total 
    		FROM {$wpdb->prefix}post_views pvc, {$wpdb->prefix}posts wpp
    		WHERE pvc.period >= %d
    			AND pvc.type = 0 
    			AND wpp.post_type = '%s'
    			AND pvc.id = wpp.ID
    		GROUP BY pvc.id 
    		ORDER BY total DESC
    		LIMIT %d";
    	
    	return $wpdb->get_results( 
    		$wpdb->prepare( 
    			$query,
    			$date_since,
    			$post_type,
    			$limit
    		) 
    	);
    }

    You can use the following in your theme to loop through the results:

    $top_views = pvc_get_posts_by_interval( 'post', 15, 20 );
    foreach ( $top_views as $post ) : setup_postdata( $top_views );
    
    	// Loop - You can use functions like the_title()
    
    endforeach;
    wp_reset_postdata();
    #21051
    Bartosz
    Keymaster

    Depending on what you’re trying to achieve you can just perform any WP_Query you like, including date query with orderby “post_views” parameter.

    When you do so, you’ll have a new “total_views” data returned along other query data, like posts, found_posts etc. and each post object extended with post_views data (a views number of this specific post).

    Example:

    $args = array(
    	'orderby' => 'post_views',
    	'order'	=> 'ASC',
    	'date_query' => array(
    		array(
    			'year'  => 2016,
    			'month' => 12,
    			'day'   => 12,
    		),
    	),
    );
    $query = new WP_Query( $args );

    – this would result in posts published in 12th december 2016 along their views data and total views data for all posts published in that period

    Additionally you can query any posts to get their views data from any period usign the views_query – a PVC extension to native WP_Query

    Example:

    $args = array(
    	'orderby' => 'post_views',
    	'order'	=> 'ASC',
    	'views_query' => array(
    		'year'  => 2016,
    		'month' => 12,
    		'day'   => 12,
    	)
    );
    $query = new WP_Query( $args );

    – this would result in posts that were viewed on 12th december 2016 along their views data and total views data in that period

    You can of course make any WP_Query combination you like.

    $args = array(
    	'orderby' => 'post_views',
    	'order'	=> 'ASC',
    	'views_query' => array(
    		'year'  => 2016,
    		'month' => 12,
    	),
    	'date_query' => array(
    		array(
    			'year'  => 2016,
    			'month' => 12,
    			'day'   => 12,
    		),
    	)
    );
    $query = new WP_Query( $args );

    – this would result in posts published on 12th december 2016 and their views data for december 2016

    #21080
    Riddler
    Participant

    Thanks, but this can’t do the same as my own function. I needed a function, that gives me the most viewed posts of the last 3, 7 or 30 days (or any other specific day count) alongside with their view count of this period.

    Your views_query example above would be great, if i could use the “after”, “before” or “compare” arguments to define time periods like in the date_query. Only yearly, monthly, weekly and daily views data is not exactly what i need.

    #21081
    Bartosz
    Keymaster

    This will definitelly be like that in the future – we just didn’t handle all types of query yet.

    #21895
    martin.krcho
    Participant

    This feature will be great. I am going to implement custom orderby attribute “views_X_days” where X is the number of days to take into account.

    #38086
    presspickle
    Participant

    did this ever happen?

    #41138
    geylang666
    Participant

    Anyone did this?

    #46176
    force400
    Participant

    I’m looking for this also, the suggestion of Martin would be great!

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.