FacetWP – sort by average rating using the WP Job Manager Reviews plugin

mgibbs189

{{gist file=”functions.php” lang=”php”}}
<?php
/**
* Add a “Rating” sort option
*
* @author David Nash <david@davidnash.com.au>
*/
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 );

{{/gist}}