Hi,
I'm having trouble applying two step sorting to my query/view.
I need to sort my output first by custom field "art-des-eintrags" then alphabetical.
"art-des-eintrags" is a numeric value, needs sorting in descending order.
It does not matter if it is a php-code or views solution ...
thanks in advance,
Vladimir
Dear Vladimir,
With Views at the moment, you can only sort by one field. So start by sorting on "art-des-eintrags" descending in the View itself. Then we can add a few lines of code to functions.php for the additional sorting:
add_filter('posts_orderby', 'adjust_order');
function adjust_order( $order ) {
if ( strpos( $order, 'meta_value_num' ) ) {
$order .= ", wp_posts.post_title ASC";
}
return $order;
}
You might need to echo $order for some trial / error to get this working. Here is another example of doing this:
https://toolset.com/forums/topic/parametric-search-display-results-by-post-type-date/#post-63488
Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Caridad
Dear CaridadZ,
your code snipped worked like a charm after I changed 'meta_value_num' to 'meta_value'
maybe because I used this one as "option" in my custom_fields.
add_filter('posts_orderby', 'adjust_order');
function adjust_order( $order ) {
if ( strpos( $order, 'meta_value' ) ) {
$order .= ", wp_posts.post_title ASC";
}
return $order;
}
Thanks for your great support.
PS: Maybe it is just me, but I can't find the search-field to search inside your forum. The one on top seems not to be working for me.
Dear Caridad,
Sorry to bother again, but now i ran into a new problem.
I need to sort all of my querys by this two values,
but my custom_taxonomy is sorted by post_date.
I managed to get it sorted ASC but, I can't figure out how to apply "art-des-eintrags" DESC then title ASC to all querys,
there must be a easy way to apply it the way you did in this filter-function.
Thanks in advance,
Vladimir
Dear Vladimir,
You can apply the filter this way:
add_filter('wpv_filter_query', 'setup_order', 10, 2);
function setup_order($q, $view) {
// adjust this condition to decide when to apply the filter
if ($view['view_id'] == 1246) {
add_filter('posts_orderby', 'adjust_order');
}
return $q;
}
function adjust_order($order) {
remove_filter('posts_orderby', 'adjust_order');
$order .= ", wp_posts.post_title ASC";
}
Remove the condition if you want it to be applied everywhere, otherwise adjust it to your needs. You can, for example, check for a certain custom post type.
Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Caridad