Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Advanced Search Filters for MyRP (1/4) WordPress


This is the first of several filters and search functions I hope to accomplish today.

The project is meant to be an advanced search page for one my custom post type. This one will be orderby and filter by review rating. The review plugin is My Review Plugin. I was not able to attach the plugin (too big) I can PM it to interested parties to help with writing the code. I also uploaded the current archive page for custom post type here:[[LINK href=""]][[/LINK]]
What I need done:

On the CPT archive page you will see this:

$guide_sort = $_GET['sorter'];


else if ($guide_sort == 'type2') { echo 'Highest Review Rating'; }

1) So what I need is filters for posts_fields , posts_join , and posts_orderby that will order the queried posts by their My Review Plugin ratings.

2) Additionally, I would like to add a checkbox form on the side that will filter out results by a minimum review rating:

So there will be 4 checkboxes: 1 star and up, 2 stars and up, 3 stars and up, 4 stars and up, in which any users with ratings below the checked box are filtered out.

Let's make this a shortcode. Also, once a user makes a selection, hide the form and show the selection (a 'remove this selection button', that would be good too)

The one file I found in the plugin that may be useful has the built in sorting features I thought might help: [[LINK href=""]][[/LINK]]

Here is the plugin API page: [[LINK href=""]][[/LINK]]

Answers (3)


Yakir Sitbon answers:

You can PM me for coding..

Kyle comments:

Other developers: After contacting Yakir, he has withdrawn


Arnav Joy answers:

can you explain your question more , please


Daniel Yoen answers:


the query would look like this :

$wpquery->request = "SELECT $wpdb->posts.* FROM $wpdb->posts
LEFT JOIN " . $wpdb->prefix . "myrp_ratings
ON $wpdb->posts.ID = " . $wpdb->prefix . "myrp_ratings.post_id
WHERE $wpdb->posts.post_type = 'guides' AND $wpdb->posts.post_status = 'publish'
ORDER BY " . $wpdb->prefix . "myrp_ratings.value";

$pageposts = $wpdb->get_results($wpquery->request, OBJECT);

foreach ($pageposts as $post)

Daniel Yoen comments:

Or you can use this filter :

function orderby_rating($orderby, $wp_query)
global $wpdb;

if ( isset( $wp_query->query['orderby'] ) && 'rating' == $wp_query->query['orderby'] )
$orderby = "(
SELECT $wpdb->posts.* FROM $wpdb->posts
LEFT JOIN " . $wpdb->prefix . "myrp_ratings
ON $wpdb->posts.ID = " . $wpdb->prefix . "myrp_ratings.post_id
WHERE $wpdb->posts.post_status = 'publish'
GROUP BY object_id)";
$orderby .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
return $orderby;
add_filter( 'posts_orderby', 'orderby_rating', 10, 2 );

then you can add :

array("orderby" => "rating", "order" => "ASC")

in your query

modified from :

Hope this help :-)

Kyle comments:

Hi, thanks for the reply

I tried the second one but when I put it removed all results from the query

I think I am going to post on for a larger prize if you are still interested

Daniel Yoen comments:


try this(function.php)

function add_new_query_vars($public_query_vars)
$public_query_vars[] = 'posts_fields';
$public_query_vars[] = 'posts_join';
$public_query_vars[] = 'posts_where';
$public_query_vars[] = 'posts_orderby';
return $public_query_vars;
function new_posts_fields($fields)
$new_fields = get_query_var('posts_fields');
if ($new_fields)
$fields .= (preg_match('/^(\s+)?,/', $new_fields)) ? $new_fields : ", $new_fields";
return $fields;
function new_posts_join($join)
$new_join = get_query_var('posts_join');
if ($new_join)
$join .= ' ' . $new_join;
return $join;
function new_posts_where($where)
$new_where = get_query_var('posts_where');
if ($new_where)
$where .= ' ' . $new_where;
return $where;
function new_posts_orderby($orderby)
$new_orderby = get_query_var('posts_orderby');
if ($new_orderby)
$orderby = $new_orderby;
return $orderby;
add_filter('query_vars', 'add_new_query_vars');
add_filter('posts_fields', 'new_posts_fields');
add_filter('posts_join', 'new_posts_join');
add_filter('posts_where', 'new_posts_where');
add_filter('posts_orderby', 'new_posts_orderby');

then, query

global $wpdb;
$args = array(
'posts_join' => " LEFT JOIN " . $wpdb->prefix . "myrp_ratings ON $wpdb->posts.ID = " . $wpdb->prefix . "myrp_ratings.post_id",
'posts_orderby' => $wpdb->prefix . "myrp_ratings.value"
$query = new WP_Query($args);

Kyle comments:

I added this code in, but did not get any sorting by review rating.

The project was moved to elance if you are interested, the prize went up to $300