Advanced sorting of posts by ratings

GD Star Rating supports expanding of WordPress Query with several variables allowing you to sort posts using ratings. It works with all types of ratings, and it’s easy to set up. But regular method can be problematic for some uses, so here is the advanced method to do same thing.

You can check out old article on GD Star Rating official website here. This one is using query_posts() function and passing parameters to it. But, this method lacks several things that you might need: paging to work for one thing. This function overwrites whole WP_Query, and since it’s run after the WP_Query is initialized, you loose some of the parameters already sate, like paging. Also, it can cause search to stop working, because of the same problem.

This advanced method is very similar, but, this time we are modifying WP_Query before the posts are retrieved in the first place. Code I am adding bellow needs to go into functions.php for a theme. For this example, variables will be set for home and search page. If you need something else you can modify the IF line to include different rules for using the sorting. This method will preserve all the other variables in WP_Query and only adds new parameters for sorting. Also, notice that I have set filter priority to 1, so that this filter function is run before same filter is running inside GD Star Rating.

[sourcecode language=”php”]<?php
add_filter(‘pre_get_posts’, ‘gdsr_get_posts’, 1);

function gdsr_get_posts($wp_query) {
if (is_home() || is_search()) {
$wp_query->set(‘gdsr_sort’, ‘thumbs’);
$wp_query->set(‘gdsr_order’, ‘desc’);

This code is sorting posts in the loop by thumbs ratings in descending order. You can change what is used for sorting by setting variables differently, and list of supported variables is bellow. You can add any of them into the function example above:

  • gdsr_sort: data used for sorting results (rating, review, votes, thumbs,┬álast_voted)
  • gdsr_order: data sorting order (asc, desc)
  • gdsr_multi: standard or multi ratings data to use (multi set id if you want to set by multi rating/review results)
  • gdsr_fsvmin: filter to specify minimal number of standard rating votes
  • gdsr_ftvmin: filter to specify minimal number of thumbs rating votes


  1. Blue Horizons Ltd says:

    I have a custom pagination function and the wp_query doesn’t change the results order as per my interpretation:

    global $paged;
    if(empty($paged)) $paged = 1;

    global $wp_query;
    $wp_query->set(‘gdsr_sort’, ‘rating’);
    $wp_query->set(‘gdsr_order’, ‘desc’);
    $wp_query->set(‘gdsr_multi’, 1);
    $pages = $wp_query->max_num_pages;
    $pages = 1;

  2. MillaN says:

    This code is not good for this. After you set gdsr variables for sorting, you need to run query again to take the sorting into account. Example in the articles is for modifying the query before the WP_Query runs the query using the filter. In your case, you don’t have that. You can use something like:


  3. Blue Horizons Ltd says:

    Thanks for that. The ordering now works. However, when I sort the results like that, I am not getting all the results back. Can you think of why this might happen?

    The test listing I am supposed to get back is 85 results over 9 pages (10 per page) but when I add the gdsr ordering I am only getting 32 results over 4 pages.

  4. Blue Horizons Ltd says:

    Just to note, I only have about 2 listings with submitted ratings so assume it isn’t to do with empty ratings!?

  5. MillaN says:

    Plugin joins posts table to the ratings table so that it can filter and sort data. But, this shouldn’t be changing results volume, since the LEFT JOIN is used to allow for post to be without ratings.

  6. Blue Horizons Ltd says:

    The only thing proceeding my $wp_query->get_posts() is the gdsr filters as per this article. Hang on a minute … if I pass gdsr_multi then will any posts that don’t have a multirating set be somehow ommitted from the post results?

    Also, it is annoying trying to leave posts/replies on these pages as it keeps asking for a rating. I have to disable javascript to be able to post anything!?

  7. Blue Horizons Ltd says:

    All articles have multirating set as default when published, so that isn’t the problem.

    The good news. Sorting with gdsr_sort and gdsr_order alone (without gdsr_mult) the number of results returned is corrrect. However, the sorting only works partly in that results that match another criteria come first … is there a way to just overwrite this?

  8. Blue Horizons Ltd says:

    Needless to say that gdsr_mult fixes this, but it filters some of the results out … hmmm.

  9. Blue Horizons Ltd says:

    I think I found the problem … I had GD Star Rating widget hidden from admin post edit pages and assumed that it would still save default. Instead, post entries made after hiding this have NOT been saved with multirating value and thus are not included with the multiset sort … re-enabling the widget and then re-saving/updating all posts seems to rectify this problem.

  10. MillaN says:

    Good thing you find the problem. I plan to have this much improved in GDSR 2.0.

Leave a Reply