Skip Navigation

[Resolved] Taxonomy numbers not correctly ordering

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

Problem:

How to order term options of taxonomy field of post form by term slug?

Solution:

It needs custom codes, for example:

https://toolset.com/forums/topic/taxonomy-numbers-not-correctly-ordering/#post-2353375

Relevant Documentation:

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

Last updated by SteBlood 2 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#2346077
taxorder.png
toolset taxonomy.png

Tell us what you are trying to do?
I've entered taxonomy numbers from 1 - 26.
However, within the dropdown on the frontend form and on the backend, the order of these numbers are not, 1 2 3 4 5 6
It's ordering them like 1 10 11 12... 2 20 21 22.

Is there a way we can get these to order single digits and THEN double digits?

Is there a similar example that we can see?
Please see the screenshots

What is the link to your site?
The website is under development at the moment and isn't accessible to the public.

#2346139

Nigel
Supporter

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

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

Hi there

The ordering you see is because the term names and slugs are evaluated as strings not numbers.

You could try to intervene in the queries that retrieve the terms to tell WordPress to treat them as numbers—except there are no query arguments to specify that: https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/#parameters

(You can specify that term meta be handled as numbers, but that doesn't apply here.)

I think the only real solution would be to edit the terms such that the slugs include preceding zeroes, e.g. 01,02,03 etc, though you can leave the display name without them.

I think the default order is based on the slugs, which would fix the problem, but if not it would be necessary to use the WordPress API to modify the relevant queries. (We can help with that if required.)

Planning ahead, if you are likely to add terms into the hundreds then you will want to include as many preceding zeroes as necessary, e.g. 001, 002, ... 010, 011, ... 100, 101 etc.

#2346307
toolsetnumber.png

Hi Nigel,

Thank you for your help.

I did find this post;

https://toolset.com/forums/topic/how-to-control-the-display-order-of-taxonomies/

However, this post seems more towards the output of the content (as a finished product) on the front end.

However, I am trying to show this as;

1 Year
and
01 Year, will not look great when output onto the frontend. It isn't very friendly to the eye.

I did try a few things, like puting letters and zeros before the numbers within the slug of the taxonomy, however, it seems that the order is based off the name as apposed to the slug itself.

If there is another way you're able to possibly help with rectifying this for me, it would be amazing.

Thank you for your time so far!

I've added a screenshot of the desired finish on the front end.

#2346727
tax-slug.jpg

Hello,

I have tried it in my localhost, it is possible to use term slug to order terms as your request, see my screenshot: tax-slug.jpg

And it works fine in the custom search form too

#2346813
toolsettermslug.png

Hi Luo,

Thank you for your input into this.

As explained previously, I've already tried this.
However I only tried with a singular double digits.

As per your screenshot, I again tried with triple digits.

Unfortunately it didn't work for me.

Please see screenshot

#2348447

I have tried it in my localhost, it works fine in both admin side and search form in frontend, if you need more assistance for it, please provide a test site with the same problem, I can setup a demo for you. Private message box enabled

#2351567
tax-slug-order.jpg

It does work in your website, see here:
1) Admin side:
hidden link
You need to order the result by taxonomy slug

2) In frontend:
hidden link
You just need to find and select the "Year" taxonomy field, choose the options: order by taxonomy slug
see my screenshot tax-slug-order.JPG

#2351659

Hi Lou,

Thanks again for going to this length.

However, I don’t use content templates or the block editor to style my front end.
I use elementor. So the option to order by slug sadly is not available for me.
Also, this doesn’t resolve the numbers showing incorrectly on the CRED form.

I’m guessing that it isn’t possible and I’m just going to have to put up with the numbers how they’ve ordered themselves.

#2352249
year-filter.jpg

It is the same steps to setup the post view with classic editor, I have setup another demo in your website:
1) Create a post view "Taxonomy numbers not correctly ordering 2" with classic editor:
hidden link
filter by year taxonomy, and order the options by term slug, see my screenshot year-filter

2) Edit the previous page with Elementor editor:
hidden link
add a "Toolset View" widget into content, choose above view

3) Test it in frontend, it works fine:
hidden link

Can you confirm it? thanks

#2352477

Hi Luo,

Thank you very much. This works a treat!

As much as this helps, it still doesn't fix my problem with the taxonomy on the CRED form showing incorrectly.

Your solution here will definitely help with my search settings using the legacy views, unfortunately, not for my actual form.
Is there anything at all I can do to have this show correctly within my CRED fom?

It really boggles my mind honestly, looking at your screenshot (https://toolset.com/wp-content/uploads/2022/04/2346727-tax_slug.jpg), Luo, you've managed to order them, however my site refuses to do this....
Have I done something wrong?

Many thanks for your amazing assistance so far.

Steve

#2353375
term-order2.jpg
term-order1.jpg

Thanks for the details, there is a misunderstanding, you are talking about custom field in Toolset post form, please check our document:
https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_field

There isn't such kind of built-in feature to order the terms by slugs, so it needs custom codes, I have set a demo in your website:
1) Dashboard-> Toolset-> Settings-> Custom code, add one item " tax-year-left-orderby-slug", with below codes:

add_action('pre_get_terms', function($query){
	$post_type = 'holiday-home';
	$taxonomy = 'year-left';
	if(!is_admin() && get_post_type() == $post_type && isset($query->query_vars['taxonomy']) && in_array($taxonomy, $query->query_vars['taxonomy'])){
		$query->query_vars['orderby'] = 'slug';
	}
	return $query;
}, 99 );

2) Create a page, display the post form "Admin add a new Holiday Home Form ", test it in frontend:
hidden link
It works fine, see my screenshot term-order1.jpg

I have tried again in your website admin side:
hidden link
It works fine in admin side too, see my screenshot term-order2.jpg

Can you confirm it? thanks

#2353725

Luo you aboslute Leg-End, God, Immortal you solved my problem!

Thank you