Skip Navigation

[Resolved] Calculated value for a Custom Field saving a Custom Type Post

This support ticket is created 3 years, 8 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/Karachi (GMT+05:00)

Author
Posts
#2095751

Hi,

I would to know how to set a calculated value for a Custom Field when saving a Custom Type Post.

I give you and example.
I've a "mysite_users" is the Custom Type Post.
The "mysite_users" has this Custom Fields:
- Name : is a text field
- Type : is a Select field with values Gold - Silver - Bronze
- Date Subscription : is a Date field
- Date Expiration : is a Date field but with automatic value based on Type field
(For example:
if type = Gold then Expiration = Date + 12 months
if type = Silver then Expiration = Date + 6 months
if type = Bronze then Expiration = Date + 1 month)

When I add a new "mysite_users" I insert:
Name = "John"
Type = "Gold"
Date Subscription : "06/21/2021"
After saving the system autofill the "Date expiration" with the value "06/21/2022" (= 06/21/2021 + 12Months)

Can you help me? Can you give an example?

Regards
Alex

#2096437

Hi Alex,

Thank you for contacting us and I'd be happy to assist.

If you're using the front-end Toolset Form to save the custom post type, you can use the "cred_save_data" hook to fire a custom function that can update the Date Expiration field:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

In case, you're saving the custom post types using both, the front-end Toolset Form as well as the admin area post edit screen, then you can alternatively use the "save_post" hook:
https://developer.wordpress.org/reference/hooks/save_post/

You'll find some relevant examples for the similar requirement in these threads:
https://toolset.com/forums/topic/cred-post-form-doesnt-fire-the-save_post-hook/
https://toolset.com/es/forums/topic/calculation-and-storing-in-a-new-field/

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar