Skip Navigation

[Resolved] Order View by Post Title THEN by custom (Toolset) field

This thread is resolved. Here is a description of the problem and solution.

Problem:

Add "Secondary sorting" of Views result with custom PHP codes.

Solution:

It is possible with custom codes, you can use wpv_filter_query to change the orderby parameter, for example:

https://toolset.com/forums/topic/order-view-by-post-title-then-by-custom-toolset-field/#post-2193557

Relevant Documentation:

This support ticket is created 3 years, 3 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 4 replies, has 2 voices.

Last updated by Kate 3 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#2192547
Sort By.png
Field Details.PNG

* Tell us what you are trying to do? *
I need to set the secondary sorting option a custom (toolset) field.
The Metakey is: wpcf-business-owner-or-team-member
The slug is: business-owner-or-team-member
Type: Checkbox

So when users view the output on this page: hidden link
The list would sort by 'Post Title' THEN by 'wpcf-business-owner-or-team-member'

Is there any documentation that you are following?
Unable to find any Views documentation 🙁

Is there a similar example that we can see?
Nope 🙁

What is the link to your site?
hidden link

Thanks so much in advance - I'd really appreciate any direction on how to make this work 🙂

#2192653

Hello,

There isn't such kind of built-in feature within Toolset Views:
1) First, in your screenshot:
https://toolset.com/wp-content/uploads/2021/10/2192547-Sort_By.png
The custom fields are not available in the option "Secondary sorting", you should be able to see this option supports only few fields

2) Second, Toolset custom checkboxes field stores value in serialized array format, you can not use it to sort view's results, in your same screenshot, option "Ordering", you will not find the checkboxes field "business-owner-or-team-member" too.

So my opinion is "not to go".

#2193451

Thanks so much Luo - apprecate your help! I had realised that there is no option, natively, to use custome fields as a second sort condition.

I've changed the custom feild to a drop down (rather than a checkbox) - i didn't realise the way these values are stored affects the way they are used.

I've set the view now to:
1) Sort by the custom field 'Business Owner or Team Member
2) Post Title (alpha)

But I really do need to reverse these.

Would there be any way to customize the Javascript to do this?

#2193557

It is possible with custom codes, you can use wpv_filter_query to change the orderby parameter, for example:

Add below codes into your theme file"functions.php":

add_filter( 'wpv_filter_query', function($query, $setting, $view_id){
	if(in_array($view_id,array(2970))){
		$query['meta_key'] = 'wpcf-' . 'business-owner-or-team-member';
		$query['orderby'] = array('title' => 'ASC', 'meta_value_num' => 'ASC');
	}
	return $query;
}, 999, 3);

Please replace 2970 with your post view's ID, replace business-owner-or-team-member with custom field slug of 'Business Owner or Team Member'

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

#2196377

My issue is resolved now. Thank you!