Auto Populate Field Based On Calculation
- This topic has 5 replies, 4 voices, and was last updated 1 year, 2 months ago by
FizzPop Media.
-
AuthorPosts
-
October 26, 2022 at 9:22 PM #38831
Zakir
ParticipantHi,
I am looking for a way to do the following
1. Create a Custom Post Type called Profile
2. Create Custom Fields, two of which will be "Date of Birth" and "Age"
3. Ask the user to input their "Date of Birth" only
4. Automatically calculate the user's age (Age = Current Date - Date of Birth) and store it in the profile's "Age" field upon submission
5. Everyday, automatically update the age field to correctly reflect the user's age
6. Use WPGridBuilder Facets to filter for Age on the frontend using the Age field which is auto calculated regularly.Is this possible?
October 27, 2022 at 12:45 PM #38845Long Nguyen
ModeratorHi,
It is possible to update a field value based on another field value when saving the post. You can use the action hook
rwmb_after_save_fieldto do that. Please read more on the documentation https://docs.metabox.io/actions/rwmb-after-save-field/October 27, 2022 at 8:01 PM #38857Zakir
ParticipantThank you for the response. So if I understand correctly, I can use a Code Snippet to write a function that checks the current date and also pulls the Date of Birth. Pseudo code below.
//Check if the month and date of the current date is equal to the month and date of the date of birth as a condition to update the age field
If ( (Month(currentDate)).(Day(currentDate)) = (Month(dateOfBirth)).(Day(dateOfBirth))) {
$age = currentDate - dateOfBirth
}do_action( 'rwmb_after_save_field', $null, $field, $age, $old, $object_id );
Is this correct?
October 28, 2022 at 10:07 AM #38867Long Nguyen
ModeratorHi,
For example: the field Date Of Birth has the ID
date_of_birthand the field Age has the IDage, the code to update the fieldageafter saving the fielddate_of_birthshould be:add_action( 'rwmb_date_of_birth_after_save_field', function ( $null, $field, $new, $old, $object_id ) { // $new is the date_of_birth field value $age_value = $new; // your function to calculate the age here update_post_meta( $object_id, 'age', $age_value); }, 10, 5 );July 17, 2023 at 10:53 PM #42610BN
ParticipantHello Zakir,
I am exactly in the same situation as your original post. Did you manage to get this done? I would appreciate if you can guide me please?
Thank you!
August 24, 2024 at 10:04 PM #46233FizzPop Media
ParticipantI am attempting to do something similar to this where I need to set the value of
total_bathswhen an entry is saved.total_bathsneeds to be the total offull_bathsandhalf_bathsCan you provide instructions on how I would update the value of
total_bathsbased on the value of the two other fields? -
AuthorPosts
- You must be logged in to reply to this topic.