How to sort posts by views, comments, rating etc wordpress

Guys im in need of a way to sort my posts by views, rating, comments. ive search loads of plugings but their all buggy.

i want something like this.
sorting http://img138.imageshack.us/img138/2577/sorting.png

Related posts

Leave a Reply

2 comments

  1. For separating the different ways of sorting you could use something like jQuery to create a tabbed area, within each you call a different (php) function to sort your posts accordingly and then define these php functions in your function.php file.

    As for the functions – wordpress already stores the number of comments on a post -but you’ll need to get it store page view / ratings. For the first, wp-postviews will work fine – we just want something to store the data. It comes with dedicated functions to get posts by popularity which you could use, but in case you wanted greater flexibility I’ve included functions below which sort by number of views or number of comments:

    For sorting by comments:

    function get_most_commented($limit=10) {
        global $wpdb;
    
       $most_commented = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts WHERE post_type='post' AND post_status = 'publish' ORDER BY comment_count DESC LIMIT 0 , $limit");
    
        foreach ($most_commented as $post) {
            setup_postdata($post);
            $id = $post->ID;
            $post_title = $post->post_title;
            $count = $post->comment_count;
            $output .= '<li><a href="'. get_permalink($id).'">'.$post_title. '</a> </li>';
        }
        return $output;
    }
    

    For sorting by post views

    function get_most_visited($limit=10) {
        global $wpdb;
    
        $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_type='post' AND post_date < '".current_time('mysql')."' AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
    
    
        foreach ($most_viewed as $post) {
                $id = $post->ID;
                $post_views = intval($post->views);
                $post_title = get_the_title($post);
                $post_title = $post->post_title;
                 $output .= '<li><a href="'. get_permalink($id).'">'.$post_title. '</a>
         }
    
        return $output;
    }
    

    Then just include those functions: get_most_visited() and get_most_commented() (with an optional argument of number of posts – default is 10) inside <ul> or <ol> tags. (I’ve included how to retrieve the number of comments/views in case you wanted to use them – otherwise you could remove them)

    This method gives you a lot of flexibility in how to present the posts. Basically – this allows you easily to style the list with some basic CSS styling or something a bit fancier involving jQuery.

    As for post ratings a plugin like Post Star Ratings might do the trick to store ratings, and then you could use a similar function to the above.

    Hope this helps!

  2. You’re going to have to write your own custom queries in WordPress. This involves getting comfortable with PHP and some WordPress API.

    A good starting point: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    As for the ways you want to sort, you’ll first need to capture views and ratings. My intuition says you can store everything in custom fields–so familiarize yourself with the post_meta table. Here are some thoughts off the top of my head:

    Most Viewed: In your single.php, everytime a post is loaded, make sure you add a custom field that increments – use this custom field in your ORDER BY query.