Fetch Posts Based on Multiple Ratings foreach Taxonomy
I am setting ratings of posts based on the taxonomies, lets say, now i want to query them based on ratings and taxonomies. an app on PC has rating 10. (meta_key=rating_pc) an app on Android has rating 5. (meta_key=rating_android)
add_action( 'posts_where', 'minscoreaction' ); function minscoreaction( $sql ){ global $wpdb; $minscore = 9;//get_query_var( 'minscore' ); $platformslug = 'pc';//get_query_var( 'platformlist' ); if( $minscore ){ $sql .= $wpdb->prepare( " AND ( $wpdb->postmeta.meta_key LIKE %s AND $wpdb->postmeta.meta_value LIKE %s )", "rating_".$platformslug.'%',$minscore.'%');
PROBLEM: I am getting nothing... means no post. And actually one post exist with rated 9 for pc.
NOTE: I can only use this method to Query Post due to several reasons.
ANOTHER NOTE:
$where .= $wpdb->prepare( " AND $wpdb->posts.post_type LIKE %s ", 'reviews%' );
This returns all the reviews Post Type But This returns nothing
$where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_value LIKE %s ", '9%' );
FRONT-END
print_r(query_posts('post_type=reviews&platformlist=pc&minscore=9'));
Answers
Use this in functions.php or your template page where you want to use.
function filter_where( $where = '' ) { global $wpdb; $minscore = 9; $platformslug = 'pc'; $posttype = "reviews"; $where .= " AND ($wpdb->posts.post_type = '".$posttype."')"; $where .= " AND (($wpdb->postmeta.meta_key LIKE '_overall_rating_".$platformslug."%' AND $wpdb->postmeta.meta_value = '".$minscore."')) "; return $where; } add_filter('posts_where', 'filter_where');
OR with WP_QUERY
use this code on frontend.
$the_query = new WP_Query(array( 'post_type' => $posttype , 'meta_query' => array( array('key' => '_overall_rating_' . $platformslug, 'value' => $minscore) ) )); print_r($the_query);