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!
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