Skip to content

Instantly share code, notes, and snippets.

@pankajpatel
Forked from mgibbs189/functions.php
Created March 14, 2016 23:38
Show Gist options
  • Save pankajpatel/94fa616c2d8faa9caa17 to your computer and use it in GitHub Desktop.
Save pankajpatel/94fa616c2d8faa9caa17 to your computer and use it in GitHub Desktop.
FacetWP - sort by average rating using the WP Job Manager Reviews plugin
<?php
/**
* Add a "Rating" sort option
*
* @author David Nash <[email protected]>
*/
function custom_wpjmr_after_save_comment_review( $comment_id ) {
$wpjmr = WP_Job_Manager_Reviews::instance();
$wpjmr->review->save_rating_average_post_meta( $comment_id );
}
add_action( 'wpjmr_after_save_comment_review', custom_wpjmr_after_save_comment_review' );
function my_facetwp_sort_options( $options, $params ) {
$options['rating'] = array(
'label' => 'Rating',
'query_args' => array(
'orderby' => 'meta_value_num', // sort by numerical custom field
'meta_key' => '_average_rating', // required when sorting by custom fields
'order' => 'DESC', // descending order
)
);
return $options;
}
add_filter( 'facetwp_sort_options', 'my_facetwp_sort_options', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment