• I am developing a business directory site and have created a simple “sort by” dropdown using the <select> tag for the frontend so users can display search results based on title, date, etc.

    I want to add the ability to sort by “highest rated” to return the businesses which have the best ratings first. I have the rating system all setup but I have been unable to add this to my sort dropdown by meta key/value.

    The meta key is “rating_avg” and the meta value is “comment_count” so I have tried the following php (bold text):

    ....
    		}
    	} elseif ( $orderby_value == 'post_title' ) {
    		$clauses['orderby'] = $wpdb->prefix.'posts.post_title';
    	} elseif ( $orderby_value == 'post_date' ) {
    		$clauses['orderby'] = $wpdb->prefix.'posts.post_date';
    	} elseif ( $orderby_value == 'post_id' ) {
    		$clauses['orderby'] = $wpdb->prefix.'posts.ID';
    	} elseif ( $orderby_value == 'random' ) {
    		$clauses['orderby'] = 'RAND()';
    	} elseif ( $orderby_value == 'most_ratings' ) {
    		$clauses['orderby'] = $wpdb->prefix.'posts.comment_count DESC';
    	} <strong>elseif ( $orderby_value == 'highest_rating' ) {
    		$meta['key'] = 'rating_avg';
    		$clauses['orderby'] = $wpdb->prefix.'posts.comment_count DESC';</strong>
    	}
    
            //return modified clauses
    	return $clauses;
    }

    I have tried a lot of different methods but cannot figure this out. The “Comment Count” works fine, but using the same structure as above how can I identify the meta key/value of the average ratings so I can use it in the dropdown?

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hey SJS719,

    So this starts to get a little tricky but goto this link:
    https://codex.www.ads-software.com/Class_Reference/WP_Query and search for “‘orderby’ with ‘meta_value’ and custom post type” I’m guessing you are using a custom post type?

    Hope that is helpful.
    –AJ

    Thread Starter SJS719

    (@sjs719)

    Hey AJ,

    Thanks for your response, but I have already read the codex for WP_Query and still cannot figure out the correct function. I have tried just about everything and still can’t get it to sort correctly.

    I have tried:

    elseif ( $orderby_value == 'highest_rating' ) {
    		$meta_key = 'rating_avg';
    $clauses['orderby'] = $wpdb->postmeta.'meta_value DESC';

    As well as:

    elseif ( $orderby_value == 'highest_rating' ) {
    		$meta_key = 'rating_avg';
    		$clauses['orderby'] = $wpdb->get_col( $wpdb->prepare(
    	"
    	SELECT      meta_key
    	FROM        $wpdb->postmeta meta_key
    	ORDER BY    meta_value
    	",
    	$meta_key
    ) );

    It seems like the orderby value “meta_value” isn’t working correctly even though I have tried multiple ways of including ‘meta_key=keyname’.

    Anyone know what I’m doing or not doing correct?

    Hi SJS719,

    OK glad you’ve read that. I think your first example above is close but I think the ‘orderby’ clause should be:

    $clauses[‘orderby’] = ‘meta_value_num’ => ‘DESC’;

    If the above doesn’t lead you to a solution it would help to see more of your code…
    –AJ

    Thread Starter SJS719

    (@sjs719)

    Hey AJ,

    Using $clauses['orderby'] = 'meta_value_num' => 'DESC'; causes a syntax error. Here is the full code:

    function gmw_orderby_filter( $clauses, $gmw ) {
    	global $wpdb;
    
    	//getting the chosen value so we could pre-select the selected value after page load
        	$selected = ( isset( $_GET['gmw_orderby'] ) && !empty( $_GET['gmw_orderby'] ) ) ? $_GET['gmw_orderby'] : '';
    
    	//check if order-by value was submitted. If it was we will use its value otherwise we set the default value to 'distance'
    	$orderby_value = ( isset( $_GET['gmw_orderby'] ) && !empty( $_GET['gmw_orderby'] ) ) ? $_GET['gmw_orderby'] : 'distance';
    
    	//check the value of the order-by and modify the clause based on that
    	//when order-by distance
    	if ( $orderby_value == 'distance' ) {
    		/*
    		 * when we do order-by distance we must check an address was entered.
    		 * we cannot order results by distance when ther is no address to calculate distance to
    		 * and so we will rsults with an error.
    		 * So we check for the address and if address found we will order results by distance.
    		 * Otherwise, we can order results by a different value. In this example i use post_title
    		 */
    		if ( isset( $gmw['org_address'] ) && !empty( $gmw['org_address'] ) ) {
    			//order by distance when address entered
    			$clauses['orderby'] = 'distance';
    		} else {
    			//if no address order by post title
    			$clauses['orderby'] = $wpdb->prefix.'posts.post_title';
    		}
    	} elseif ( $orderby_value == 'post_title' ) {
    		$clauses['orderby'] = $wpdb->prefix.'posts.post_title';
    	} elseif ( $orderby_value == 'post_date' ) {
    		$clauses['orderby'] = $wpdb->prefix.'posts.post_date DESC';
    	} elseif ( $orderby_value == 'post_id' ) {
    		$clauses['orderby'] = $wpdb->prefix.'posts.ID';
    	} elseif ( $orderby_value == 'random' ) {
    		$clauses['orderby'] = 'RAND()';
    	} elseif ( $orderby_value == 'most_ratings' ) {
    		$clauses['orderby'] = $wpdb->prefix.'posts.comment_count DESC';
    	} elseif ( $orderby_value == 'highest_rating' ) {
    		$meta_key = 'rating_avg';
    		$clauses['orderby'] = $wpdb->postmeta.'meta_value DESC';
    }
    
            //return modified clauses
    	return $clauses;
    }
    add_filter( 'gmw_pt_location_query_clauses', 'gmw_orderby_filter', 20, 2 );

    I am using the plugin “GEO My WordPress” so that is why ‘gmw’ appears throughout the code.

    Thread Starter SJS719

    (@sjs719)

    Anyone?

    If you’ve taken a look at the WP_Query article in the codex why have you not swapped over your queries to follow the WP Coding standards?

    It looks like you’re sticking to querying the database using standard sql queries instead of utilizing the WP_Query class.

    Also, you may want to take a look at the pre_get_post hook.

    Thread Starter SJS719

    (@sjs719)

    Hi Evan,

    I am just trying to comply with the plugin by using the same formatting which they use. I think the reason they do not follow the standard WP_Query coding standards is because the Geo My WP plugin uses its own separate database to store its data.

    I do not have the knowledge to build this query from scratch so I am trying to find a way to simply include a meta sort ability in this pre-built code.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to Sort Query Results By Meta Key/Value?’ is closed to new replies.