Skip Navigation

[Fermé] How to compare custom fields and display the one with the highest value?

This support ticket is created Il y a 2 années et 7 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 1 réponse, has 2 voix.

Last updated by Nigel Il y a 2 années et 7 mois.

Auteur
Publications
#2165249

Tell us what you are trying to do?
I want to compare a group of custom fields then display the value of the custom field with the highest value.

Here is an example:
I have 5 custom fields to show what % of people take which transport to work.

Here are my 5 custom fields and each of them has a value associated with them.
cf-transport-uber - 10
cf-transport-car - 50
cf-transport-bus - 20
cf-transport-train - 15
cf-transport-bike - 5

I want to compare all these 5 custom fields and show the custom field with the highest value. In this case, it is cf-transport-car.

Can Toolset do this?

One idea that I have is by using conditional WHAT/IF outputs.

#2165461

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

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

For something like this you should really register a custom shortcode.

That shortcode would get each of the custom field values for the current post, determine the highest, and return which field it belongs to (or a user-friendly text version, e.g. "Car"), depending on your needs.

The tricky-ish part is knowing which field the high value belongs to, and I'd probably put the fields (the slug, or a text label) together with the values in an array and sort the array by values, so that the key of the first element of the sorted array is the text you want to return.

See
https://developer.wordpress.org/plugins/shortcodes/
https://developer.wordpress.org/reference/functions/get_post_meta/
hidden link

Le sujet ‘[Fermé] How to compare custom fields and display the one with the highest value?’ est fermé à de nouvelles réponses.