Ordering by highest rating with WP_Query

This topic contains 7 replies, has 3 voices, and was last updated by  Aleks Szymanski 4 years, 9 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket

Tagged: , ,

  • Author
    Posts
  • #497842

    Aleks Szymanski
    Free User
    Post count: 23

    How do I order places by highest rating? I have this but it doesn’t seem to work:

    $posts = new WP_Query(array(
    	'post_type' => 'gd_place',
    	'posts_per_page' => 5,
    	'orderby' => 'high_rating'
    ));
    #497855

    Aleks Szymanski
    Free User
    Post count: 23

    Note: I’m trying to accomplish this on a regular WP page template, not an geo directory template. I’ve even tried this:

    $posts = new WP_Query(array(
    	'post_type' 		=> 'gd_place',
    	'posts_per_page' 	=> 5,
            'sort_by' 		=> 'overall_rating_desc',
    ));
    #497869

    Alex Rollin
    Moderator
    Post count: 27815

    Hello,

    GD uses custom tables and we don’t have an integration with that plugin.

    Have you looked at the plugin code? You can check the widgets like GD Listings and GD Loop to see how our queries are created.

    #498008

    Aleks Szymanski
    Free User
    Post count: 23

    Thanks Alex. Is there any way one of your developers can provide a simple solution for ordering by highest rating using a simple WP_Query? I checked widget code and it’s a little above my pay-grade. 🙂

    Maybe a simple filter I can add to functions.php?

    #498038

    Alex Rollin
    Moderator
    Post count: 27815

    I have already flagged your topic for the developers who will respond as soon as possible.

    #498045

    Aleks Szymanski
    Free User
    Post count: 23

    Awesome, thanks so much!

    #498181

    Kiran
    Moderator
    Post count: 7069

    Hello Aleks,

    Currently there is no way to retrieve listings via custom WP_Query. You can use REST API http://www.YOUR-SITE.COM/wp-json/geodir/v2/places/?orderby=overall_rating_desc&order=desc

    Kiran

    #498428

    Aleks Szymanski
    Free User
    Post count: 23

    Thanks Kiran! I’ll check that out.

    For now, I did end up finding a solution:

    
    
    // Main args
    
    	$post_type = 'gd_place';
    	
    	$args = array(
    		'post_type' 		=> $post_type,
    		'posts_per_page' 	=> 5
    	);
    	
    // Sort by highest rated using GeoDirectory query args filter
    
    	$temp_args = $args;
    	
    	$geodir_sort = 'high_rating'; // or 'overall_rating_asc'
    	
    	$test_instance = array (
    		'post_limit' 	=> 5,
    		'post_type' 	=> $post_type,
    		'sort_by' 		=> $geodir_sort
    	);
    	
    	$temp_args['is_geodir_loop'] = true;
    	$temp_args['order_by'] 		= $geodir_sort;
    	
    	$temp_args = apply_filters( 'geodir_widget_listings_query_args', $temp_args, $test_instance );
    	$widget_listings = geodir_get_widget_listings( $temp_args );
    	
    	$post_ids = array();
    	foreach( $widget_listings as $listing ) {
    		$post_ids[] = $listing->ID;
    	}
    
    	$args['post__in'] 	= $post_ids;
    	$args['order'] 		= 'DESC';
    	$args['orderby'] 	= 'post__in';
    	
    // The loop
    	
    	$places = new WP_Query($args);
    	
    	if ( $places->have_posts() ) :
    		while ( $places->have_posts() ) : $places->the_post();
    			echo '<div class="listing"><div class="the-title">'.get_the_title().'</div></div>';
    		endwhile; wp_reset_postdata();
    	endif;

    Basically you build your query args, retrieve post ids using geodir widget query args filter, and then pull those into the main args using “post__in”.

    Hope this helps someone else in the future!

Viewing 8 posts - 1 through 8 (of 8 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket