Skip Navigation

[Resolved] Taxonomies Numerical Sorting issue

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.

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 3 months ago.

Author
Posts
#2739546
Screenshot-2024-09-04-at-4.26.01-PM.jpg
Screenshot 2024-09-04 at 4.17.54 PM.png
Screenshot 2024-09-04 at 4.17.41 PM.png

My post-type archive page has a custom search filter. The numeric sorting isn't what I am looking for.

I want the dropdown to look like this:
Number of Entry Codes.

25

50

1000

1200

3000

But it currently looks like this:
Number of Entry Codes.

1000

1200

25

3000

50

Can you please let me know how to set this sorted correctly?
Thank you

#2739835

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

The problem is that these taxonomy terms are numbers, but when they are retrieved using the standard function get_terms to populate the dropdown options, they are evaluated as strings.

Although the number 10 comes after the number 9 the string "10" comes before the string "9".

The arguments for the get_terms function don't offer the ability to handle the terms as numbers rather than strings, hence there is no such option in the settings for a taxonomy search filter.

The way to avoid this problem is to use leading zeros in the slugs of the terms, and then order the terms by the slug.

So if you have a term "100" you would set its slug as "00100". Likewise, if you have a term "99" you would set its slug as "00099".

Ordering by slug as strings "00099" comes before "00100", which is what you want, and the dropdown will display the term names, so it will display 99 before 100.

#2740123

Thank you that fixed it!