WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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

add_filter('posts_orderby', 'edit_posts_orderby');

    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;
share|improve this question

Simply wrap the hooks in a conditional check something like:

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

and leave the rest as is.

share|improve this answer
Bainternet, thank you for your time! I added that code and it helps the issue of categories not breaking, but the sorting no longer works on the home page. Maybe it doesn't know it's my home page? – DreamGrowers Dec 19 '12 at 6:09
But more importantly, I'm wondering if you might know how to modify this code to work on the home page as it did AND work on the categories. I will gladly paypal you $15 to help me get this working. Please send your paypal info. :) – DreamGrowers Dec 19 '12 at 6:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.