Skip Navigation

[Resolved] Secondary sort with distance sort

This thread is resolved. Here is a description of the problem and solution.

Problem:
The customer asked:

1. How to set the view's order by title first and then when the distance-based search is performed, order by the searched location.

2. How to show some content based on the existence of the distance value from the searched location.

Solution:
1. Suggested customizing the order settings using the "wpv_filter_query" filter and shared the code example.

2. Suggested using the "toolset-maps-distance-value" shortcode inside the conditional statement and shared an example.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#toolset-maps-distance-value

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.02 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by davidG-5 1 year, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2403033

Tell us what you are trying to do?
I have 2 problems.

1) I have created a store locator, the client wants to be able to have it arrange but title initially until a location is searched then it needs to order but distance, I can do one or the other but with the distance sort it removes the secondary sort option.

2) I want the distance to show either with an append or have a conditional for if the distance is blank it doesnt display the content like the standard conditionals can but for that specific value.

Is there any documentation that you are following?
Just a toolset page

Is there a similar example that we can see?

What is the link to your site?
hidden link

#2403559

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

distance-order-settings-example.png

Hi,

Thank you for contacting us and I'd be happy to assist.

1. The ordering that you're trying to achieve is not possible using the view's ordering settings, so it will require some custom code.

The "wpv_filter_query" hook can be used to set the ordering by 'title', when the distance search is not used, for example:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )


add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
	// process if specific view
	if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
		// if distance search is not set
		if(empty($_GET['toolset_maps_distance_radius'])) {
			// set order by title
			$query_args['orderby'] = 'title';
			$query_args['order'] = 'ASC';
		}
	}
	return $query_args;
}

Note: Please replace '12345' with the actual ID of your view.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

When the page will load, the results will be ordered by the title, but, once the distance search will be used, the results will show, ordered by the distance.

Note: Please make sure that the view's ordering settings are set to order by distance, as shown in the attached example screenshot.

2. To conditionally show the distance from the searched location, you can use the "toolset-maps-distance-value" shortcode inside the conditional statement like this:
( ref: https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#toolset-maps-distance-value )


[wpv-conditional if="( '[toolset-maps-distance-value origin_source='url_param' postmeta='wpcf-product-address']' ne '' )"] 
      Distance from search location:<br>[toolset-maps-distance-value origin_source='url_param' postmeta='wpcf-product-address'] km.
[/wpv-conditional]

Note: You'll replace "product-address" with the actual slug of your address field and please also add "toolset-maps-distance-value" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2403581

Amazing thank you that was exactly what I needed

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.