Thank you, that approach will help to make the results a little better than what I have now.
I have been working on a custom sorting code for this view, but I think that there is an error in how I'm initiating the function. I'm not seeing any difference in the results with this code activated or not.
I would appreciate any guidance you can offer.
function custom_workshop_sorting( $query_args, $view_settings, $view_id, $view_slug ) {
if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 233717) ) {
// ----------------------------------------------------------------------
// 1. CONFIGURATION: UPDATED VALUES
// ----------------------------------------------------------------------
// The slug and ID of your specific Toolset View
$target_view_slug = 'workshop-search-in-person-2';
$target_view_id = 233717;
// The raw meta key for your Workshop's Start Date custom field (e.g., 'wpcf-start-date')
$start_date_meta_key = 'wpcf-start-date';
// The URL parameter used when an origin address is known
$origin_address_param_name = 'toolset_maps_distance_center';
// The name of the query parameter Toolset uses to pass the user's explicit sort choice.
$user_sort_param_name = 'wpv_sort_orderby';
// The value Toolset uses in the URL when the user selects the date sort option.
$date_sort_option_value = 'field-' . $start_date_meta_key;
// ----------------------------------------------------------------------
// Get input values from the URL query string ($_GET)
$origin_address_center = isset( $_GET[ $origin_address_param_name ] ) ? sanitize_text_field( $_GET[ $origin_address_param_name ] ) : '';
$has_origin_address = ! empty( $origin_address_center );
$user_sort_choice = isset( $_GET[ $user_sort_param_name ] ) ? sanitize_key( $_GET[ $user_sort_param_name ] ) : '';
// CHECK 1: Has the user explicitly selected the Date sort option?
$user_selected_date_sort = ( $user_sort_choice === $date_sort_option_value );
// --- CONDITION 1: No origin address provided ($origin_address_param_name is empty) ---
if ( ! $has_origin_address ) {
// Sort results by start date (numerically, for timestamp/date format)
$query_args['orderby'] = 'meta_value_num';
$query_args['meta_key'] = $start_date_meta_key;
$query_args['order'] = 'ASC';
return $query_args;
}
// --- Conditions where an origin address IS provided ($has_origin_address is TRUE) ---
// Clear conflicting sorting keys when using compound sorting array
$query_args['order'] = '';
$query_args['meta_key'] = '';
if ( $user_selected_date_sort ) {
// --- CONDITION 3: Address entered AND user chose to sort by start date (via dropdown) ---
// Sort by Start Date (Primary), then by Distance (Secondary)
$query_args['orderby'] = array(
$start_date_meta_key => 'ASC', // Primary: Start Date (uses raw meta key for WP_Query)
'distance' => 'ASC' // Secondary: Distance (Toolset internal)
);
} else {
// --- CONDITION 2: Address entered AND user chose the default/address string sort ---
// This is the fallback for all non-date choices when an address is present.
// Sort by Distance (Primary), then by Start Date (Secondary)
$query_args['orderby'] = array(
'distance' => 'ASC', // Primary: Distance (Toolset internal calculation)
$start_date_meta_key => 'ASC' // Secondary: Start Date (uses raw meta key for WP_Query)
);
}
}
return $query_args;
}
add_filter( 'toolset_views_query_args', 'custom_workshop_sorting', 10, 4 );