Advertisement
geminilabs

[site-reviews] sort posts by rank with Elementor Pro

Jan 13th, 2020 (edited)
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. /**
  2.  * This will sort posts added to your page with the Elementor Posts widget by
  3.  * rank, assuming that you are assigning reviews to the post_type chosen in the
  4.  * "Query > Source" option of the widget. In order for this to work, you will also
  5.  * need to set the "Query > Query ID" option of the widget to: "sort-by-rank".
  6.  *
  7.  * If you would prefer to sort by rating instead of rank,
  8.  * replace "_glsr_ranking" with "_glsr_average" in the function.
  9.  *
  10.  * @param \WP_Query $query
  11.  * @return void
  12.  */
  13. add_action('elementor/query/sort-by-rank', function ($query) {
  14.     $query->set('meta_query', [
  15.         'relation' => 'OR',
  16.         ['key' => '_glsr_ranking', 'compare' => 'NOT EXISTS'], // this comes first!
  17.         ['key' => '_glsr_ranking', 'compare' => 'EXISTS'],
  18.     ]);
  19.     $query->set('orderby', 'meta_value_num');
  20. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement