WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Setting Time Parameter to WP-PostViews Plug In (5 posts)

  1. laulaulau
    Member
    Posted 4 years ago #

    Hi, I'm fairly new to using wordpress. I'm using the WP-PostViews plugin from here.

    What I'm able to do is grab the top five posts according to views. My question is how do I set a time parameter of 2 months, so it displays the most viewed posts from only the last 2 months?

    If this is not possible, is there another plug-in out there that can do this?

    Here's the code in my footer.php

    <?php if (function_exists('get_most_viewed')): ?>
    <h3>Popular</h3>
    <ul>
       <?php get_most_viewed('post',5); ?>
    </ul>
    <?php endif; ?>

    Here's the code of the function in the plug-in php

    ### Function: Display Most Viewed Page/Post
    if(!function_exists('get_most_viewed')) {
    	function get_most_viewed($mode = '', $limit = 10, $chars = 0, $display = true) {
    		global $wpdb, $post;
    		$views_options = get_option('views_options');
    		$where = '';
    		$temp = '';
    		$output = '';
    		if(!empty($mode) && $mode != 'both') {
    			$where = "post_type = '$mode'";
    		} else {
    			$where = '1=1';
    		}
    		$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");
    		if($most_viewed) {
    			foreach ($most_viewed as $post) {
    				$post_views = intval($post->views);
    				$post_title = get_the_title();
    				if($chars > 0) {
    					$post_title = snippet_text($post_title, $chars);
    				}
    				$post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
    				$post_content = get_the_content();
    				$temp = stripslashes($views_options['most_viewed_template']);
    				$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp);
    				$temp = str_replace("%POST_TITLE%", $post_title, $temp);
    				$temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp);
    				$temp = str_replace("%POST_CONTENT%", $post_content, $temp);
    				$temp = str_replace("%POST_URL%", get_permalink(), $temp);
    				$output .= $temp;
    			}
    		} else {
    			$output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
    		}
    		if($display) {
    			echo $output;
    		} else {
    			return $output;
    		}
    	}
    }
  2. Lester Chan
    Member
    Posted 4 years ago #

    It is not possible. WP-PostViews does not logged data, it just accumulate the views.

  3. dlawton
    Member
    Posted 3 years ago #

    Have been looking for the same thing for a while, I am able to show top rated posts sorted by date using gd star rating plugin, but can't sort most viewed by date, although I love this plugin it would be a thousand times more powerful with date filtering.

    Anyone know of a way to do this, preferably without having to migrate data?

    Thank you very much!
    Dave

  4. Amine27
    Member
    Posted 3 years ago #

    Hi,

    I added there lines to get posts from now to the last year in my case:

    } else {
    			$where = '1=1';
    		}
    		$start_date = mktime(0,0,0,date("m"),date("d"),date("Y")-1);
    		$date_limit = "post_date BETWEEN '".date("Y-m-d", $start_date)."' AND '".current_time('mysql')."'";
    
    		$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 $date_limit AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER  BY views DESC LIMIT $limit");
    		if($most_viewed) {

    in get_most_viewed function, for tow months the code became:

    $start_date = mktime(0,0,0,date("m")-2,date("d"),date("Y"));

  5. dlawton
    Member
    Posted 3 years ago #

    @Amine27

    Thanks, interesting to see that it is possible, I'll have to try this out. Ideally I need to have a parameter to select certain time periods so that the user can select from a few different periods (day/month/week for example). It would be great if this could be incorporated into the plugin itself, but otherwise I'll follow your lead and try to hack it :)

    Cheers!

Topic Closed

This topic has been closed to new replies.

About this Topic