diff --git a/includes/classes/class-settings-panel.php b/includes/classes/class-settings-panel.php index 31c9affa4f..3bda30bfbd 100644 --- a/includes/classes/class-settings-panel.php +++ b/includes/classes/class-settings-panel.php @@ -1687,7 +1687,8 @@ public function prepare_settings() 'popular', 'price_low_high', 'price_high_low', - 'random' + 'random', + 'distance', ], 'options' => [ [ @@ -1722,6 +1723,10 @@ public function prepare_settings() 'value' => 'random', 'label' => __('Random listings', 'directorist'), ], + [ + 'value' => 'distance', + 'label' => __('Distance', 'directorist'), + ], ], ], 'display_view_as' => [ @@ -3129,7 +3134,8 @@ public function prepare_settings() 'popular', 'price_low_high', 'price_high_low', - 'random' + 'random', + 'distance', ], 'options' => [ [ @@ -3163,6 +3169,10 @@ public function prepare_settings() [ 'value' => 'random', 'label' => __('Random listings', 'directorist'), + ], + [ + 'value' => 'distance', + 'label' => __('Distance', 'directorist'), ], ], ], diff --git a/includes/helper-functions.php b/includes/helper-functions.php index 5bb72acf83..622c8e893c 100644 --- a/includes/helper-functions.php +++ b/includes/helper-functions.php @@ -1539,6 +1539,7 @@ function atbdp_get_listings_orderby_options($sort_by_items) 'price-asc' => __("Price (low to high)", 'directorist'), 'price-desc' => __("Price (high to low)", 'directorist'), 'rand' => __("Random listings", 'directorist'), + 'distance' => __("Distance", 'directorist'), ); $sort_by_items = is_array( $sort_by_items ) ? $sort_by_items : []; if (!in_array('a_z', $sort_by_items)) { @@ -1565,6 +1566,9 @@ function atbdp_get_listings_orderby_options($sort_by_items) if (!in_array('random', $sort_by_items)) { unset($options['rand']); } + if (!in_array('distance', $sort_by_items)) { + unset($options['distance']); + } $args = array( 'post_type' => ATBDP_POST_TYPE, 'post_status' => 'publish', diff --git a/includes/model/Listings.php b/includes/model/Listings.php index 1f4ed459ac..92d46f5647 100644 --- a/includes/model/Listings.php +++ b/includes/model/Listings.php @@ -164,7 +164,7 @@ public function set_options() { $this->options['listings_display_filter'] = get_directorist_option( 'home_display_filter', 'sliding' ); $this->options['listing_filters_fields'] = get_directorist_option( 'listing_filters_fields', array( 'search_text', 'search_category', 'search_location', 'search_price', 'search_price_range', 'search_rating', 'search_tag', 'search_custom_fields', 'radius_search' ) ); $this->options['listing_filters_icon'] = get_directorist_option( 'listing_filters_icon', 1 ) ? true : false; - $this->options['listings_sort_by_items'] = get_directorist_option( 'listings_sort_by_items', array( 'a_z', 'z_a', 'latest', 'oldest', 'popular', 'price_low_high', 'price_high_low', 'random' ) ); + $this->options['listings_sort_by_items'] = get_directorist_option( 'listings_sort_by_items', array( 'a_z', 'z_a', 'latest', 'oldest', 'popular', 'price_low_high', 'price_high_low', 'random', 'distance' ) ); $this->options['disable_list_price'] = get_directorist_option( 'disable_list_price' ); $this->options['listings_view_as_items'] = get_directorist_option( 'listings_view_as_items', array( 'listings_grid', 'listings_list', 'listings_map' ) ); $this->options['display_sort_by'] = get_directorist_option( 'display_sort_by', 1 ) ? true : false; @@ -224,7 +224,7 @@ public function update_search_options() { $this->options['view_as_text'] = get_directorist_option( 'search_viewas_text', __( 'View As', 'directorist' ) ); $this->options['listings_view_as_items'] = get_directorist_option( 'search_view_as_items', array( 'listings_grid', 'listings_list', 'listings_map' ) ); $this->options['sort_by_text'] = get_directorist_option( 'search_sortby_text', __( 'Sort By', 'directorist' ) ); - $this->options['listings_sort_by_items'] = get_directorist_option( 'search_sort_by_items', array( 'a_z', 'z_a', 'latest', 'oldest', 'popular', 'price_low_high', 'price_high_low', 'random' ) ); + $this->options['listings_sort_by_items'] = get_directorist_option( 'search_sort_by_items', array( 'a_z', 'z_a', 'latest', 'oldest', 'popular', 'price_low_high', 'price_high_low', 'random', 'distance' ) ); $this->options['order_listing_by'] = apply_filters( 'atbdp_default_listing_orderby', get_directorist_option( 'search_order_listing_by', 'date' ) ); $this->options['sort_listing_by'] = get_directorist_option( 'search_sort_listing_by', 'asc' ); $this->options['listing_columns'] = get_directorist_option( 'search_listing_columns', 3 ); @@ -431,7 +431,7 @@ public function get_review_data() { } private function execute_meta_query_args(&$args, &$meta_queries) { - if ( 'rand' === $this->orderby ) { + if ( in_array( $this->orderby, [ 'rand', 'distance' ] ) ) { $current_order = atbdp_get_listings_current_order( $this->orderby ); } else { $current_order = atbdp_get_listings_current_order( $this->orderby . '-' . $this->order ); @@ -656,6 +656,14 @@ private function execute_meta_query_args(&$args, &$meta_queries) { $args['orderby'] = 'rand'; } break; + case 'distance': + if ( $this->has_featured ) { + $args['meta_key'] = '_featured'; + $args['orderby'] = 'meta_value_num distance'; + } else { + $args['orderby'] = 'distance'; + } + break; } }