Support » Plugin: WP Most Popular » Selecting popular posts based on views AND comments

  • Hi

    I’ve always been a fan of WP Most Popular but found myself wishing it could determine popularity by a combination of views AND comments, so I made a quick and dirty hack to the $result definition in helpers.php – posting here in case someone else might find it useful.

    Here’s what I’ve got:

    $result = $wpdb->get_results( $wpdb->prepare( "
    		SELECT p.*
    		FROM {$wpdb->prefix}most_popular mp
    		INNER JOIN {$wpdb->prefix}posts p ON mp.post_id = p.ID
    			p.post_type = '%s' AND
    			p.post_status = 'publish' AND
    			DATE(p.post_date) > CURDATE() - INTERVAL 30 DAY
    		ORDER BY (comment_count * 5) + 30_day_stats DESC
    		LIMIT %d
    	", array( $post_type, $limit ) ), OBJECT );

    The code weighs comment_count five-to-one against views which seems to work well for the traffic/interaction our site gets – but you may need to experiment with the value in (comment_count * 5) to suit your own.

    Also I’ve hardcoded the SELECT range to the past 30 days to prevent the results from including super-old posts with high comment counts – there’s probably a much better way to do this but it seems to work.


  • The topic ‘Selecting popular posts based on views AND comments’ is closed to new replies.