A well-designed WordPress website prioritizes its content that is of most importance to its users. This may involve sorting posts based on its relevance and popularity (e.g. highest ratings).
There are several factors that can be used to determine the popularity of a post such as ratings, number of votes, comment activity, page views and also time.
This article describes how you can sort posts by highest ratings, utilising the Multi Rating Pro WordPress plugin.
Sort Posts in The WordPress Loop by Highest Ratings
WordPress themes and plugins typically use a helper class called WP_Query to fetch post data. The WordPress Loop is the code which WordPress uses to iterate and display posts or pages. The WP_Query is a pre-requisite to initialize the WordPress Loop.
Here’s an example of the WordPress loop.
Multi Rating Pro provides an option which modifies the main WP_Query class to automatically sort posts by highest ratings.
Top Rated Posts or Highest Ratings Widget
Multi Rating Pro provides a widget which allows you to display a list of top rated posts. Add the Rating Result List Widget and ensure the sort by option is set to “Highest Rated”. You can also limit the number of posts to be fetched and filter posts by specific taxonomies and terms.
Sort Posts by Highest Ratings Via URL Query String Variables
Multi Rating Pro stores a copy of calculated ratings in a couple of postmeta fields including the meta key “mrp_rating_result_<rating_form_id>_star_rating” where <rating_form_id> is the rating_form id e.g. “mrp_rating_result_1_star_rating”. This postmeta field stores an overall star rating value.
orderby is the argument passed to WP_Query to tell it what column to sort on when it is creating the ORDER BY clause for its generated SQL. The default value for orderby is post_date. However you can specify a postmeta field to use instead using the following syntax:
As per the WordPress Codex, query variables can be passed to the WP_Query via URL query string parameters. Note that when pretty permalinks are enabled, the query_vars filter must be used to make any query variables available to the WP_Query.
For example, the following URL will sort posts by highest rated: