Order by ranking / reviews
-
Hi
Just installed your plugin, and it looks really nice.
I want to use it to show the best rated companies.I have added the code that shows the stars on my site.
Now I just need to sort them, so the 5 stars are on top, then the 4 stars and so on.The developer of the review systems gives this information:
“The Custom Field to sort by is _glsr_ranking”
Don’t know how to use that information.
Is there an easy way to sort the order by star rank?The page I need help with: [log in to see the link]
-
Hi,
the plugin for site reviews uses a private custom post type named “site-review” and currently my plugin Posts in Sidebar does not support private custom post types.Starting from the next version I’ve added support for this types of posts, so that you will be able to get these posts and sort them by ratings.
Please, give me some days, so that I can test my last modifications. After having published the new version, I’ll tell you how to do this.
Thanks for your appreciated request.
- This reply was modified 6 years ago by Aldo Latino.
Hi,
I just released the 4.7.3 version which addresses this particular request.In order to display the custom post types created by the plugin “Site Reviews” (the plugin you’re using) and sort them by the rating value, follow these steps:
1) Create a new Post in Sidebar widget;
2) In Getting posts > Post type, chooseReview
;
3) In Getting posts > Get posts with this meta key, enterrating
;
4) In Getting posts > Order posts by, chooseMeta value number
;
5) In Displaying posts > The custom fields > Display a single custom field, activate the optionDisplay the custom field of the post
;
6) In Displaying posts > The custom fields > Display a single custom field > Display this custom field, chooserating
;
7) In Displaying posts > The custom fields > Display a single custom field > Use this text before the custom field, enterRating (stars):
or whatever you like.Let me know, please.
@aldolat Hi, this is the author of the Site Reviews plugin.
Here is some additional information of how to display (and sort by rank) pages which have reviews assigned to them:
- Create a new Post in Sidebar widget
- Choose the post type that has reviews assigned to them (i.e. Page)
- In Getting posts > Get posts with this meta key:
_glsr_ranking
- In Getting posts > Order posts:
Meta value number
- In Getting posts > The order will be:
DESC
This will display the pages that have reviews assigned to them and sort them by ranking. The algorithm that determines ranking is the same as that used by IMDB to rank rated films.
However, this will only show pages that have reviews assigned to them.
In order to show ALL pages (regardless of whether or not they have reviews assigned to them) and sort them by rank, you would need to perform the following WP_Query:
$pageQuery = new WP_Query([ 'meta_query' => [ 'relation' => 'OR', ['key' => '_glsr_ranking', 'compare' => 'EXISTS'], ['key' => '_glsr_ranking', 'compare' => 'NOT EXISTS'], ], 'order' => 'DESC', 'orderby' => 'meta_value_num', 'post_status' => 'publish', 'post_type' => 'page', 'posts_per_page' => 10, ]);
How can this be done with the “Post in Sidebar” widget?
I have tried using the “Custom fields query” but it doesn’t seem to work (the custom fields query does not appear to be used in the SQL query).
Hi @geminilabs,
please give me some time, because I am currently very busy.
Thank you for your patience. ??Sure, no problem.
To help speed things up, here is the reason why it is impossible to do the query shown above with “Posts in Sidebar”:
The meta query that is required (as shown above) does not care about a specific meta_value, it just needs to check the existence and non-existence of a meta_key.
However, “Posts in Sidebar” requires that a meta query includes both the meta_key and the meta_value, otherwise it will not use the meta query.
This is the code excerpt in
pis-functions-queries.php:223
that is preventing the meta query:if ( '' === $args['mq_key_aa'] || '' === $args['mq_value_aa'] ) { $meta_query = ''; }
- This reply was modified 5 years, 9 months ago by Gemini Labs.
Hi @geminilabs,
In Getting posts > Get posts with this meta key:
_glsr_ranking
I haven’t this meta key on my test sites. I noticed this when I wrote my first post; for this reason I suggested to use the meta key
rating
.Let me know, please.
This is the code excerpt in
pis-functions-queries.php:223
that is preventing the meta query […]You’re right. Thank you for this. I am thinking to change this to:
if ( '' === $args['mq_key_aa'] ) {
The
_glsr_ranking
meta_key only exists on posts or pages that have reviews assigned to them. It does not appear on the reviews themselves.For example, [site_review_form assign_to=123] will assign all reviews submitted in the review form to the page or post with the ID of 123.
Since a page can have multiple reviews assigned to them, the
_glsr_ranking
meta_key allows to query the pages with assigned reviews and sort them by rank.The difference between the two examples is that your example sorts reviews by rating, and my example sorts pages by their rank based on the reviews that have been assigned to them.
Since only pages that have reviews assigned to them will have the
_glsr_ranking
meta_key, this is why the meta query in my example first looks to see if the meta_key exists, and then looks to see if it does not exist. This allows the query to return all pages, showing the ones that have reviews assigned to them first and sorted by rank.- This reply was modified 5 years, 9 months ago by Gemini Labs.
@geminilabs I updated the development version of this plugin. Now, only the
meta_key
is required for the meta query.Now Posts in Sidebar can get posts/pages with the meta key
_glsr_ranking
without specifying the value.Can you try installing this development version in a test environment? Let me know, please.
@aldolat Yes it works.
The only thing to be aware of (that I just realised) is that in order to sort pages that have ranking BEFORE pages that do not, you need to use the “NOT EXISTS” before the “EXISTS”. Otherwise WordPress will not sort the order correctly.
The reason for this is because WordPress uses the meta_value of the last “LEFT JOIN” statement in the “ORDER BY” clause.
For example, this is how the query should be:
$pageQuery = new WP_Query([ 'meta_query' => [ 'relation' => 'OR', ['key' => '_glsr_ranking', 'compare' => 'NOT EXISTS'], // this comes first! ['key' => '_glsr_ranking', 'compare' => 'EXISTS'], ], 'order' => 'DESC', 'orderby' => 'meta_value_num', 'post_status' => 'publish', 'post_type' => 'page', 'posts_per_page' => 10, ]);
And using your plugin, the widget settings should be the following:
Getting Posts
Post type:Page
(change this as needed)
Order posts:Meta value number
Custom fields query
Relation between Column A and Column B:OR
Custom field key A1:_glsr_ranking
Operator:NOT EXISTS
Custom field key B1:_glsr_ranking
Operator:EXISTS
- This reply was modified 5 years, 9 months ago by Gemini Labs.
- This reply was modified 5 years, 9 months ago by Gemini Labs.
- This reply was modified 5 years, 9 months ago by Gemini Labs.
Very clever solution! ??
Thank you so much.
- The topic ‘Order by ranking / reviews’ is closed to new replies.