Skip to content

Instantly share code, notes, and snippets.

@danielpowney
Last active April 27, 2018 14:06
Show Gist options
  • Save danielpowney/c7dbbf191966c166f63c to your computer and use it in GitHub Desktop.
Save danielpowney/c7dbbf191966c166f63c to your computer and use it in GitHub Desktop.
Search form for Multi Rating Pro. The code snippet below allows the user to select an overall star rating as search criteria in the search form. The star ratings are rounded half up or half down e.g. an overall star rating of 4.67/5 will show up in search results for 5 stars.
<?php
/**
* Search results are rounded half up or half down e.g. an overall rating of 4.67 will show up in
* search results for 5 stars.
*
* @param unknown $where
* @param unknown $query
* @return string
*/
function mrp_search_posts_where( $where, $query ) {
if ( isset( $_REQUEST['mrp-search'] ) && ! is_admin() && $query->is_search && $query->is_main_query() ) {
$search = intval( $_REQUEST['mrp-search'] );
$rating_form_id = 1;
$filters_hash = MRP_Multi_Rating_API::get_filters_hash( array() ); // if you want to add any Multi Rating pro specific filters e.g. rating_item_ids
$where .= ' AND rr.rating_form_id = ' . $rating_form_id . ' AND rr.filters_hash = "' . $filters_hash . '"'
. ' AND ROUND( rr.adjusted_star_result ) = ' . $search;
}
return $where;
}
add_filter( 'posts_where' , 'mrp_search_posts_where', 10, 2 );
/**
*
* Joins Multi Rating Pro tables for search form
*
* @param unknown $join
* @param unknown $query
* @return string
*/
function mrp_search_posts_join( $join, $query ) {
if ( isset( $_REQUEST['mrp-search'] ) && ! is_admin() && $query->is_search && $query->is_main_query() ) {
global $wpdb;
$join .= ' LEFT JOIN ' . $wpdb->prefix . MRP_Multi_Rating::RATING_RESULT_TBL_NAME . ' rr ON ' . $wpdb->posts . '.ID'
. ' = rr.post_id AND rr.rating_item_id IS NULL AND rr.rating_entry_id IS NULL';
}
return $join;
}
add_filter('posts_join', 'mrp_search_posts_join', 10, 2);
/**
* The template for displaying search forms
*
* TODO: Copy the code below and put it in a file called searchform.php in your theme.
*/
?>
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
<div>
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
<label for="mrp-search" style="margin-top: 5px; margin-bottom: 5px;"><?php _e( 'Overall Star Rating' ); ?></label>
<select name="mrp-search" style="margin-top: 5px; margin-bottom: 5px;">
<option value="0"><?php _e( 'No stars' ); ?></option>
<option value="1"><?php _e( '1 star' ); ?></option>
<option value="2"><?php _e( '2 stars' ); ?></option>
<option value="3"><?php _e( '3 stars' ); ?></option>
<option value="4"><?php _e( '4 stars' ); ?></option>
<option value="5"><?php _e( '5 stars' ); ?></option>
</select>
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment