Skip Navigation

[Resolved] Secondary Sorting by a custom field

This support ticket is created 4 years, 12 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 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/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 4 years, 12 months ago.

Assisted by: Luo Yang.

Author
Posts
#1235117

Hello, I'm trying to follow this support thread but can't seem to get this code to work for me in my child theme functions.php file:

add_filter('wpv_filter_query', 'add_custom_fiels_to_view_query', 99, 3);
function add_custom_fiels_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id == 1062 ){
return;
}

$meta_query = array();
$meta_query[] = array('key' => 'wpcf-company');
$meta_query[] = array('key' => 'wpcf-last-name');
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, mt1.meta_value, post_date ASC';
}

I have 4 or 5 views that need to follow this sort order-- by company name and then by last name. Both are custom fields for a post type. What do you think I'm missing? Thanks!

#1235385

Hello,

I suggest you follow our document to setup your custom PHP codes:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

And you can setup "orderby" parameter with multiple multiple custom field values, see the example of WordPress document:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
section: 'orderby' with multiple 'meta_key's

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