Hello!
i have "Events" with a date and a Number field ("reihenfolge") = sort order with numbers from 1-11
now i want the view to order with date and then with "reihenfolge"
i have tried this:
// SECODARY ORDNER EVENTS
add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id == 2754 ){
$meta_query = array();
$meta_query[] = array('key' => 'wpcf-datum');
$meta_query[] = array('key' => 'wpcf-reihenfolge');
if ( !isset($query_args['meta_query']) ){
$query_args['meta_query'] = array();
}
$query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
add_filter('posts_orderby', 'custom_order');
}
return $query_args;
}
function custom_order($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value ASC, mt1.meta_value ASC';
}
Here you can see the view (the little white numbers in the Corder of the Images are the number field "Reihenfolge"
hidden link
You can already sort by more than one ordering argument in the native Views Ordering options, but only the primary supports custom fields.
To allow custom fields ordering also on the secondary options you'll have to suggest this to the Product Management here:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/
We can assist only limitedly in enhancing Views with Custom code.
https://toolset.com/toolset-support-policy/
However, this is a widely requested feature and we elaborated often on it, with custom code samples.
I think the best example is this https://toolset.com/forums/topic/need-help-with-a-secondary-orderby-on-a-view/#post-415478
Please try that out, and let me know if you need an explanation on its usage, I think however Nigels' explanation is well documented.