Skip Navigation

[Resolved] Can I sort a Query View differently before and after the query has been used?

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 3 voices.

Last updated by Minesh 2 years ago.

Assisted by: Minesh.

Author
Posts
#2531573

I have a view that outputs locations. I have locations ordered by the address field ascending as a distance from URL parameter toolset_maps_distance_center

This is great for after a visitor uses the query to find the locations that are closest to them, but before the user queries by location I have all of the locations visible. The problem is that the locations appear random when you first visit the page as they're sorted by distance from the center of the map.

Is it possible to have the locations show in Alphabetical order before the user interacts with the query and then order by toolset_maps_distance_center after the user queries by their location?

Thanks!

#2531835

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

You could use the wpv_filter_query API hook to modify the query sort setting, checking $_GET to see if the toolset_maps_distance_center parameter is provided, and if not switching to order by post title.

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

#2537457

Hi Nigel - thanks for your response.
My understanding of Toolset / JS is only ok and I'm hoping you can help me with this.

I think if I set the view to be alphabetical by default, and then paste something like this in the JS it will reorder on search:

jQuery(document).ready(function($) {
	if (isset($_GET['id'])){
    $query->set('order', 'ASC');
    $query->set('orderby', 'toolset_maps_distance_center');
    }
}

Assuming this is on the right track I think I'm missing some of the $query->set parameters, such as how do I tell it to sort by the address field in the orderby section?

Thanks for your help!

#2537647

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You can not mix Server side script (PHP) and Javascript that runs of frontend. What you are doing is totally wrong.

As I understand - you want to display the view results alphabetically when page is loaded first time and when user change any filter you want to order view results by toolset_maps_distance_center - is that correct? If yes:

Can you please share on what page you added your view and share admin access details and let me review your structure first and then I'll be able to guide you with the right direciton.

*** Please make a FULL BACKUP of your database and website.***
Additionally - I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2538441

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've added the following code to the "Custom Code" section offered by Toolset with the code snippet "tcode":
=> hidden link

add_filter( 'wpv_view_settings', 'func_sort_map_by_distance', 10, 2 );
 function func_sort_map_by_distance( $view_settings, $view_id ) {
  global $WP_Views;
  $views = array( 3429 );
  if ( in_array( $view_id, $views) 
      and isset($_GET['toolset_maps_distance_center']) 
      and !empty($_GET['toolset_maps_distance_center'])) {
    
    $view_settings['distance_order']['source'] = 'url_parameter'; 
    $view_settings['distance_order']['url_parameter'] = 'toolset_maps_distance_center'; 
   
    
   
  }
  return $view_settings;
}

Do you see it working as expected now.