Stefan
I have written a new code sample for you to try. I don't have the same set-up as you with your custom post types and fields etc., so I haven't tested it, but I think it should work.
Notes:
- you should double-check the slugs for your custom fields. Note that they are stored in the wp_postmeta table with a "wpcf-" prefix, so I am using "wpcf-content-type" etc.
- I don't know what your custom field types are. I've set them all as STRING but maybe they need to be changed to, for example, NUMERIC
- take note of the where the orderby query arg is defined. This means the results are ordered by type, then platform, then lob, and then the post title. I don't know whether you want them in ASC or DESC order, you will need to adjust
And note that there is an important limitation here. This alters the query generated by your view. You can use it in a normal view, but if you try to use it on a parametric search then modifying the search filters will interfere with the customisation we have created and it will break.
Here's the code, let me know how you get on with it:
/**
* Custom sorting of portfolio, pages, and posts
*/
function views_custom_sort($query_args, $views_settings, $view_id) {
$target_types = array( 'post', 'page', 'portfolio' );
$target_views = array( '2568', '1900', '1893' );
if ( in_array( $view_id, $target_views ) ) {
$query_args['post_type'] = $target_types;
$query_args['meta_query'] = array(
'relation' => 'AND',
'type_q' => array(
'key' => 'wpcf-content-type',
'type' => 'STRING',
'compare' => 'EXISTS',
),
'platform_q' => array(
'key' => 'wpcf-content-platform',
'type' => 'STRING',
'compare' => 'EXISTS',
),
'lob_q' => array(
'key' => 'wpcf-content-lob',
'type' => 'STRING',
'compare' => 'EXISTS',
)
);
$query_args['orderby'] = array(
'type_q' => 'ASC',
'platform_q' => 'DESC',
'lob_q' => 'ASC',
'title' => 'DESC'
);
}
return $query_args;
}
add_filter( 'wpv_filter_query', 'views_custom_sort', 101, 3);
If you want to learn more see this WordPress announcement: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/