How to Sort posts by Vote Count?

Does anyone know how to automatically sort posts by Vote Count?

I am using a plugin called WP Voting where a visitor can vote on a post. The plugin keeps track of how many votes each post has. The author offered some code that would sort them posts in the main blog page and it works, but it unfortunately broke the Category view.

Read More

The code is below and it is dropped into the functions.php

/**
 * SORTS THE POSTS BY VOTE COUNT
 */
add_filter('posts_orderby', 'edit_posts_orderby');
add_filter('posts_join_paged','edit_posts_join_paged');

    function edit_posts_join_paged($join_paged_statement) {
    global $wpdb;
    $join_paged_statement = "LEFT JOIN ".$wpdb->prefix."wpv_voting ON ".$wpdb-                >prefix."wpv_voting.post_id = $wpdb->posts.ID";
    return $join_paged_statement;
}

function edit_posts_orderby($orderby_statement) {
    global $wpdb;
     $orderby_statement = "(".$wpdb->prefix."wpv_voting.vote_count) DESC";
    return $orderby_statement;
}

Related posts

Leave a Reply

1 comment

  1. Simply wrap the hooks in a conditional check something like:

    if (is_home() || is_front_page()){
        add_filter('posts_orderby', 'edit_posts_orderby');
        add_filter('posts_join_paged','edit_posts_join_paged');
    }
    

    and leave the rest as is.