Skip Navigation

[Resolved] Sort query by custom field then title

This support ticket is created 10 years, 6 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
- 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 -
- - - - - - -

Supporter timezone: Europe/Madrid (GMT+01:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Caridad 10 years, 6 months ago.

Assisted by: Caridad.

Author
Posts
#100088

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

#100115

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

#100136

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.

#100269

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

#100568

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

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