Function Reference: geodir_posts_orderby

Summary

Listing orderby filters.

Global Values

$wpdb
(object) (required) WordPress Database object.

Default: None
$wp_query
(object) (required) WordPress Query object.

Default: None
$plugin_prefix
(string) (required) Geodirectory plugin table prefix.

Default: None
$table
(string) (required) Listing table name.

Default: None

Package

GeoDirectory

Parameters

$orderby
(string) (required) The orderby query string.

Default: None

Return Values

(string)
  • Modified orderby query.

Change Log

Since: 1.0.0

Filters

‘geodir_posts_order_by_sort’ [Line: 491]

Source File

geodir_posts_orderby() is located in geodirectory-functions/listing_filters.php [Line: 345]

Source Code

function geodir_posts_orderby($orderby)
{
    global $wpdb, $wp_query, $geodir_post_type, $table, $plugin_prefix, $snear, $default_sort;

    $sort_by = '';
    $orderby = ' ';

    if (get_query_var('order_by'))
        $sort_by = get_query_var('order_by');

    /*if(isset($wp_query->tax_query->queries) && $wp_query->tax_query->queries){
        $current_term = $wp_query->get_queried_object();
    }

    if(isset($current_term->term_id)){

        $current_term->term_id;

        if(geodir_get_tax_meta($current_term->term_id,'ct_cat_sort')){
            $sort_by = geodir_get_tax_meta($current_term->term_id,'ct_cat_sort');
        }
    }*/


    if ($snear != '') {
        $orderby .= " distance,";
    }

    if (isset($_REQUEST['sort_by']) && $_REQUEST['sort_by'] != '' && is_main_query())
        $sort_by = esc_attr($_REQUEST['sort_by']);


    if ($sort_by == '') {
        $default_sort = geodir_get_posts_default_sort($geodir_post_type);
        if (!empty($default_sort))
            $sort_by = $default_sort;
    }

    /*
    if search by term & no location then order always "relevance"
    if search by location then order always "nearest"
    */
    if (is_main_query() && geodir_is_page('search')) {
        $search_term = get_query_var('s');

        if (trim($search_term) != '' && !isset($_REQUEST['sort_by'])) {
            $sort_by = 'az';
        }

        if ($snear != '' &&  $sort_by!='farthest') {
            $sort_by = 'nearest';
        }
    }

    switch ($sort_by):
        case 'newest':
            $orderby = "$wpdb->posts.post_date desc, ";
            break;
        case 'oldest':
            $orderby = "$wpdb->posts.post_date asc, ";
            break;
        case 'low_review':
        case 'rating_count_asc':
            $orderby = $table . ".rating_count ASC, " . $table . ".overall_rating ASC, ";
            break;
        case 'high_review':
        case 'rating_count_desc':
            $orderby = $table . ".rating_count DESC, " . $table . ".overall_rating DESC, ";
            break;
        case 'low_rating':
            $orderby = "( " . $table . ".overall_rating  ) ASC, " . $table . ".rating_count ASC,  ";
            break;
        case 'high_rating':
            $orderby = " " . $table . ".overall_rating DESC, " . $table . ".rating_count DESC, ";
            break;
        case 'featured':
            $orderby = $table . ".is_featured asc, ";
            break;
        case 'nearest':
            $orderby = " distance asc, ";
            break;
        case 'farthest':
            $orderby = " distance desc, ";
            break;
        case 'random':
            $orderby = " rand(), ";
            break;
        case 'az':
            $orderby = "$wpdb->posts.post_title asc, ";
            break;
        // sort by rating
        case 'overall_rating_desc':
            $orderby = " " . $table . ".overall_rating DESC, " . $table . ".rating_count DESC, ";
            break;
        case 'overall_rating_asc':
            $orderby = " " . $table . ".overall_rating ASC, " . $table . ".rating_count ASC, ";
            break;
        default:

            break;
    endswitch;

    if ($sort_by != '' && geodir_cpt_has_rating_disabled($geodir_post_type)) {
        if (in_array($sort_by, array('high_review', 'rating_count_desc', 'high_rating', 'overall_rating_desc'))) {
            $orderby = "$wpdb->posts.comment_count DESC, ";
            $sort_by = 'comment_count_desc';
        } else if (in_array($sort_by, array('low_review', 'rating_count_asc', 'low_rating', 'overall_rating_asc'))) {
            $orderby = "$wpdb->posts.comment_count ASC, ";
            $sort_by = 'comment_count_asc';
        }
    }

    global $s;

    if (is_search() && isset($_REQUEST['geodir_search']) && $s && trim($s) != '') {
        $keywords = explode(" ", $s);
        if(is_array($keywords) && $klimit = get_option('geodir_search_word_limit')){
            foreach($keywords as $kkey=>$kword){
                if(geodir_utf8_strlen($kword)<=$klimit){
                    unset($keywords[$kkey]);
                }
            }
        }
        if ($sort_by == 'nearest' || $sort_by == 'farthest') {
            if (count($keywords) > 1) {
                $orderby = $orderby . " ( gd_titlematch * 2 + gd_featured * 5 + gd_exacttitle * 10 + gd_alltitlematch_part * 100 + gd_titlematch_part * 50 + gd_content * 1.5) DESC, ";
            } else {
                $orderby = $orderby . " ( gd_titlematch * 2 + gd_featured * 5 + gd_exacttitle * 10 + gd_content * 1.5) DESC, ";
            }
        } else {
            if (count($keywords) > 1) {
                $orderby = "( gd_titlematch * 2 + gd_featured * 5 + gd_exacttitle * 10 + gd_alltitlematch_part * 100 + gd_titlematch_part * 50 + gd_content * 1.5) DESC, " . $orderby;
            } else {
                $orderby = "( gd_titlematch * 2 + gd_featured * 5 + gd_exacttitle * 10 + gd_content * 1.5) DESC, " . $orderby;
            }
        }
    }

    /**
     * Filter order by SQL.
     *
     * @since 1.0.0
     * @param string $orderby The orderby query string.
     * @param string $sort_by Sortby query string.
     * @param string $table Listing table name.
     */
    $orderby = apply_filters('geodir_posts_order_by_sort', $orderby, $sort_by, $table);

    $orderby .= $table . ".is_featured asc, $wpdb->posts.post_date desc, $wpdb->posts.post_title ";

    return $orderby;
}