• Resolved cselin

    (@cselin)


    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?

    • This topic was modified 6 years, 8 months ago by cselin.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Milan Petrovic

    (@gdragon)

    It should work, your code is OK. But, WP_Query can be filtered by other plugins or other code that hooks into the WP_Query to modify all the arguments. You should print_r the WP_Query object to see exactly how the arguments look like after you get the results. If they have changed, the problem is that something filtered the arguments.

    Milan

    Thread Starter cselin

    (@cselin)

    Thanks for your response. If I print_r the wpquery object, from these args:

    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] => product-category [field] => slug [terms] => meat-products ) ) )

    `
    WP_Query Object ( [query] => 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] => product-category [field] => slug [terms] => meat-products ) ) ) [query_vars] => 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] => product-category [field] => slug [terms] => meat-products ) ) [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [lazy_load_term_meta] => 1 [update_post_meta_cache] => 1 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [taxonomy] => product-category [term] => meat-products ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [relation] => AND [0] => Array ( [taxonomy] => product-category [terms] => Array ( [0] => meat-products ) [field] => slug [operator] => IN [include_children] => 1 ) ) [relation] => AND [table_aliases:protected] => Array ( [0] => wp_term_relationships ) [queried_terms] => Array ( [product-category] => Array ( [terms] => Array ( [0] => meat-products ) [field] => slug ) ) [primary_table] => wp_posts [primary_id_column] => ID ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => [meta_table] => [meta_id_column] => [primary_table] => [primary_id_column] => [table_aliases:protected] => Array ( ) [clauses:protected] => Array ( ) [has_or_relation:protected] => ) [date_query] => [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (98,99,100,101) ) AND wp_posts.post_type = ‘kmgf_product’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘acf-disabled’ OR wp_posts.post_status = ‘future’ OR wp_posts.post_status = ‘draft’ OR wp_posts.post_status = ‘pending’) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 9 [posts] => Array ( [0] => WP_Post Object ( [ID] => 555 [post_author] => 3 [post_date] => 2018-03-04 13:26:54 [post_date_gmt] => 2018-03-04 03:26:54 [post_content] =>

    Plugin Author Milan Petrovic

    (@gdragon)

    I have found the problem in the GD Rating System plugin, and it will be fixed in the next major version (to be released in the next 3-4 days).

    Thread Starter cselin

    (@cselin)

    Thank you! Looking forward to the update.

    Plugin Author Milan Petrovic

    (@gdragon)

    This should be fixed in just released 2.4 version.

    Thread Starter cselin

    (@cselin)

    Thanks, the new version has resolved the problem.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Ordering by star rating in Ajax’ is closed to new replies.