Ordering by star rating in Ajax
-
I have got a custom query working to display the top rated products on one of my templates, like this:
<?php $rated_prods = new WP_Query(array( 'post_type' => 'kmgf_product', 'posts_per_page' => 3, 'orderby' => 'gdrts', 'order' => 'DESC', 'gdrts_method' => 'stars-rating' )); ?>
This works great. This site also has ajax filtering options with some content on some pages. I would like the users to see the result of their filtering, sorted by rating. I try to use the same syntax in my query for AJAX, but the end results just come out in date order instead of rating order. This query is set up in functions.php and is called with ajax:
$post_type = (isset($_POST['post_type'])) ? $_POST['post_type'] : 'posts'; $ppp = (isset($_POST['ppp'])) ? $_POST['ppp'] : 9; $term = (isset($_POST['term'])) ? $_POST['term'] : 0; $taxonomy = (isset($_POST['taxonomy'])) ? $_POST['taxonomy'] : 0; $offset = (isset($_POST['offset'])) ? $_POST['offset'] : 0; $orderby = (isset($_POST['orderby'])) ? $_POST['orderby'] : 'date'; $args = array( 'post_type' => $post_type, 'posts_per_page' => $ppp, 'orderby' => $orderby, 'offset' => $offset, 'order' => 'DESC', 'gdrts_method' => 'stars-rating' ); //for each taxonomy, add it and its terms to the query args if(!empty($taxonomy)) { $taxArgs = array( 'relation' => 'AND' ); // loop through taxonomy structure and append to query foreach ($taxonomy as $tax) { $thisTax = array( 'taxonomy' => $tax[0], 'field' => 'slug', 'terms' => $tax[1] ); array_push($taxArgs, $thisTax); } $args['tax_query'] = $taxArgs; } $postsQuery = new WP_Query($args);
This code works – except for the order – it filters using values passed to it by my js code using ajax. The ‘orderby’ value is gdrts.
and example of the arguments that get used for this query in practice are:
Array ( [post_type] => kmgf_product [posts_per_page] => 9 [orderby] => gdrts [offset] => 0 [order] => DESC [gdrts_method] => stars-rating [tax_query] => Array ( [relation] => AND [0] => Array ( [taxonomy] => restrictions [field] => slug [terms] => soy-free ) ) )
SO I’m stuck – why does my template code work, but the code used in functions.php to perform a similar query not work? Why can’t I get posts in star rating order?
- The topic ‘Ordering by star rating in Ajax’ is closed to new replies.