Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/sort by distance #1312

Open
wants to merge 3 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions includes/classes/class-settings-panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,8 @@ public function prepare_settings()
'popular',
'price_low_high',
'price_high_low',
'random'
'random',
'distance',
],
'options' => [
[
Expand Down Expand Up @@ -1722,6 +1723,10 @@ public function prepare_settings()
'value' => 'random',
'label' => __('Random listings', 'directorist'),
],
[
'value' => 'distance',
'label' => __('Distance', 'directorist'),
],
],
],
'display_view_as' => [
Expand Down Expand Up @@ -3129,7 +3134,8 @@ public function prepare_settings()
'popular',
'price_low_high',
'price_high_low',
'random'
'random',
'distance',
],
'options' => [
[
Expand Down Expand Up @@ -3163,6 +3169,10 @@ public function prepare_settings()
[
'value' => 'random',
'label' => __('Random listings', 'directorist'),
],
[
'value' => 'distance',
'label' => __('Distance', 'directorist'),
],
],
],
Expand Down
4 changes: 4 additions & 0 deletions includes/helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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',
Expand Down
14 changes: 11 additions & 3 deletions includes/model/Listings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
}
}

Expand Down