If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 1 of 1
  1. #1
    Junior Member
    Join Date
    Jun 2015
    Member #

    Angry Sorting Data by Meta Value??

    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 name is "Highest Rating" so I have tried the following php which was provided by the plugin author of Geo My Wordpress (highest rating in bold text):

    PHP Code:
    function gmw_orderby_filter$clauses$gmw ) {

    //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';
    B]} elseif ( $orderby_value == 'highest_rating' ) {
    $meta_key 'rating_avg';
    $clauses['orderby'] = $wpdb->postmeta.'meta_value_num DESC';

    //return modified clauses
    return $clauses;
    All of the other filters including "most ratings" work fine, but I cannot get "highest rating" to work no matter how I structure the code. It has been pointed out to me that this code is not up to the WP_Query standards, but this is the code provided by the plugin author, and since I don't have the knowledge to recreate this function following the WP standards I am just looking for a way to keep the existing code format and add the meta key/value.

    I have tried every single combo of identifiers I can come up with to properly identify the meta key, value and location, but I am drawning a blank.

    Anyone got any ideas I could try?


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
All times are GMT -6. The time now is 04:22 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: