Skip Navigation

[Resolved] sorting by letters und numbers at the same time

This support ticket is created 4 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.

Our next available supporter will start replying to tickets in about 1.97 hours from now. Thank you for your understanding.

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: Africa/Casablanca (GMT+01:00)

This topic contains 6 replies, has 2 voices.

Last updated by Jamal 4 years, 6 months ago.

Assisted by: Jamal.

Author
Posts
#1625637

I am trying to sort a view by a certain custom field which includes numbers and letters. The order I wish to have is the following:
5a, 5b, 5c, 5d, 6a, 6b, 6c, 6d,.....up till 11a, 11b, and so on.

When I know sort by number oder numerical order it shows:
5d, 5b, 5a, 5c, 5e - which seems to me like only the number counts - not the letter
When I sort by string or native it shows:
10a, 10b,...11a,11b,...5a, 5b, 5c, 5d - which makes somehow sense, but it does not consider the absolute value of the numbers.

So what can I do?

Thanks in advance!

here is the link to the project, section "Klassenlehrkräfte" is the one it is about: hidden link

#1626319

Hello and thank you for contacting the Toolset support.

I believe that your first order(by number) returned the results ordered by their ID, which is a number.
The second search is alphabetical, which means that "10e" will come before "5e" because the comparison is done on a character per-character basis, (1<5).

I might suggest a solution but I did not test it. Please let me know what you will get and we can continue further.
1. You will need to hook into the view query, using the "wpv_filter_query". Check the documentation and a simple example here https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
2. Update the query order parameters to use UNSIGNED as the type of value, as described in the documentation of WP_Query https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

An example code would be:


// order by numbers inside strings.
add_filter( 'wpv_filter_query', 'prefix_order_by_numbers_inside_strings' );
 
function prefix_order_by_numbers_inside_strings( $query_args, $view_settings,$view_id ) {
    
	if ( $view_id = 123 ) { // ID of the view to be modified
		$query_args['orderby'] = 'meta_value';           // Ordering by custom field 
		$query_args['order'] = 'ASC';                    // ASC order
		$query_args['meta_key'] = 'custom-field-slug';   // this could be wpcf-name for a Toolset field called name
		$query_args['meta_value'] = 'UNSIGNED';          // Casting the value of custom field to take only numbers
	}
    return $query_args;
}

Please adapt this code based on your view ID(123) and field slug(custom-field-slug). Note that Toolset custom fields slugs are prefixed with 'wpcf- when using WordPress API directly.

I hope this helps. Let me know your findings.

#1628699

Hi Jamal,

I 'm not sure, if I did the right thing, but using your code with the ID and the right cf-slug crushes the site - not the whole one, but the one with the loop.

btw: I am using impreza theme and when I use the build-in grid function to show my cpts I also can sort by a certain custom field. In this case I select the specific one and the same issue occurs.

#1630937

Hello,

I can't really tell if this code will crash the website, I did not test it. But if you allow me temporary access to your website(WordPress+FTP) and specify which file did you update. Maybe there was a semicolon error or something else.

In any way, to investigate a crashed website, you will need to activate PHP debugging and check the generated errors.
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/

Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1632095

Hello, I confirm the access is working, but just after login in I encounter a security issue from the website certificate and I am unable to continue debugging. Check this screenshot from 3 browsers. hidden link

Please check with your hosting provider and let me know when I can access it again, or share a Duplicator copy of your website and I'll check it locally.

#1632263

Hi, this is because no SsL certificate is installed. I think your browsers are warning you automatically.

#1632303

Indeed! Thanks to my colleagues, I fixed it following this article hidden link

The code crashes because the function expects 3 arguments but only the first was given. I had to change the first line of code with:

add_filter( 'wpv_filter_query', 'prefix_order_by_numbers_inside_strings', 30, 3 );

You can check it on Toolset->Settings->Custom Code(tab).

I believe this has sorted the results bu the first number. But, maybe, this solution is not complete, as "5b" and "5e" are before "a". Check this screenshot hidden link

If you want to have 5a, 5b, 5c, etc. I believe that you will have to add a custom numeric field "order", enter a value for each post and use it in the view.

I hope this helps. Let me know your feedback.