Hello
I am trying to create a ranking system based on the total of 4 criteria in custom toolset fields.
Each criteria would have a corresponding number between 1-100
Fore instance I would have:
Criteria 1 - 67
Criteria 2 - 42
Criteria 3 - 98
Criteria 4 - 67
Total score - 68.6 (Total divided by 4)
I will have these custom fields on all the entries in a custom post type "Countries"
I would then want to display these Countries in order from highest score to lowest score.
Can you point me in the right direction on how I would go about accomplishing this?
Thanks
Hi, there's no calculation system built into Views or Types so you'll have to create something on your own using custom code. Furthermore, if you want to be able to sort or filter a View using that calculated value, the calculated value must be saved as a custom field. Here are some hints.
- To get the raw numeric value from a number custom field, you can use get_post_meta() - https://developer.wordpress.org/reference/functions/get_post_meta/
- To set the raw numeric value based on some calculation, you can use update_post_meta() - https://codex.wordpress.org/Function_Reference/update_post_meta
- In the database, Types fields use the prefix "wpcf-". So if your field has a slug in wp-admin like "criteria-1", then in the database that field key will be "wpcf-criteria-1".
- To calculate a field value automatically whenever the post is saved, you could use the save_data hook. https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
Humm that looks way too complicated. I guess I will just add the values manually for now.
Thanks