Skip Navigation

[Resolved] How can I find the last value used in a sequential custom field for a post type

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

Last updated by PaulS4783 3 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1962105

Let's say I have a custom post type "Clients"

And one of the custom fields of the post type is "Contact ID Number".

As we add new Client records we would like to assign a new unique "Contact ID Number" to each one.
The numbers should be sequential.
And we would like WordPress to automatically assign the "Contact ID Number" when the new Client record is saved.

There wouldn't be a huge number of Client records.
Less than 100 I imagine.

So one way I guess that this could be accomplished is to add a custom code snippet such that:
- When a Client record is saved, the system loops through all the existing Client records, finds the one with the highest value, increments that value by "1", and saves that value into the custom field of the new record.

Any suggestions on how to make that work with ToolSet?

thanks.

#1962283

Hello,

There isn't such kind of built-in feature within Toolset plugins, so you will need to consider custom codes.

For example, after user save a "Clients" post, use WP action hook "save_post" to trigger a PHP function:
https://developer.wordpress.org/reference/hooks/save_post/
In this PHP function, setup the "Contact ID Number" value as what you want:
https://developer.wordpress.org/reference/functions/update_post_meta/

One issue needs to pay attention:
If the "Contact ID Number" custom field is created with Toolset Types plugin, Types plugin will prepend slug "wpcf-" before the field slug, so you need to add "wpcf-" into the custom field meta_key, for example: wpcf-contact-id-number

#1962391

Thank you for that reply.
Yes, I was aware of those things.

My question was really "how can i write a function that loops through all the records of a custom post type AND accesses the value of a custom field in order to determine what the highest value is."

#1963473

As I mentioned above, you will need consider custom codes, for example:
Get highest "Contact ID Number" value from "Clients" posts
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
‘meta_value_num‘ – Order by numeric meta value
https://developer.wordpress.org/reference/classes/wp_query/#pagination-parameters
posts_per_page here you can use value 1

#1963657

Can you give me a hint on how to loop through all the records of a post type?

#1963799

According to our support policy, we don't provide custom codes:
https://toolset.com/toolset-support-policy/

And I have searched it in Google, found this thread with existed custom codes:
https://wordpress.stackexchange.com/questions/242789/how-to-add-a-custom-field-and-incrementing-value-for-all-the-published-posts

For your reference.

#1964987

Thanks. I will do further research on my own.