Skip Navigation

[Resolved] How to Get Child Post Fields to Inherit Values of Parent Post Fields by Default

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to Get Child Post Fields to Inherit Values of Parent Post Fields by Default

Solution:
You need to use CRED hook cred_save_data to add/update values for your parent and child post type.

You can find proposed solution with the following reply:
=> https://toolset.com/forums/topic/how-to-get-child-post-fields-to-inherit-values-of-parent-post-fields-by-default/page/2/#post-581256

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 7 years, 1 month 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.

Our next available supporter will start replying to tickets in about 2.00 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Tagged: 

This topic contains 22 replies, has 2 voices.

Last updated by jonB-5 7 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#579662

I have a number of custom post types set up as follows...

Vessel
Voyage (Child of Vessel)
Ticket (Child of Voyage)
Variation (Child of Ticket)
Booking (Child of Variation)

Vessel as a field titled vessel-authorised-reseller whose values (User IDs) are dynamically generated via checkboxes on a CRED Form that list Users with a specific User Role.

Now, what I'd like to do is have those values copied to the equivalent field voyage-authorised-reseller in any Child posts (Voyages) of a Vessel by default upon creation of a Child.

If possible, I'd like it to be such that when the values are changed in a Vessel, the values of the field in the in the Child Voyages would also be updated, but that any changes to Child Voyages would not influence the values in the Parent.

I would then likely create equivalent fields in each of the other child types to inherit from their parents.

Any advise on achieving the above would be most appreciated, and, if php is required, examples would be beneficial.

#579751

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - As I understand you would like to update/save the field voyage-authorised-reseller in any Child posts (Voyages) of a Vessel by default upon creation of a Child using CRED form or you are thinking to update while operating such posts from admin?

#579805

We would need the functionality to work through CRED forms as this is how our users will create the Child Voyages.

#579810

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You need to use CRED hook cred_save_data to update values for your parent and child post type.

Now, what I'd like to do is have those values copied to the equivalent field voyage-authorised-reseller in any Child posts (Voyages) of a Vessel by default upon creation of a Child.
==> If you want to access the parent ID for child entry you can get it using postmeta key. When using Types posts relationships, the parent of a given type is stored in a field:

_wpcf_belongs_{parent-posttype-slug}_id

To get a parent ID of post type Voyage which is setup as child of Vessel post type it will be stored as:

 _wpcf_belongs_vessel_id

So, to update post meta while creation of child post, you should get the custom field value stored for parent post type and add/update the same value for your child post type using update_post_meta() function.

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
[ You should refer to the examples given with the hook to know how you can update the custom field values. ]

#579837

Okay, I'm using the below code but with no luck...

// Enter Parent Vessel Authorised Resellers into Voyage fields when New Voyage is Created
add_action('cred_save_data', 'save_vessel_resellers_to_child_voyages',10,2);
function save_vessel_resellers_to_child_voyages($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==194)
        $parent_id = $_GET['_wpcf_belongs_vessel_id'];
	 $auth_resellers = get_post_meta($parent_id, 'wpcf-vessel-authorised-reseller', true);
	 {
	 // Enter Child Voyage Post Meta
	 update_post_meta($_POST , 'wpcf-voyage-authorised-resellers', $auth_resellers);
        }
}

To clarify, what I'm trying to make happen here is the following...

When creating a Child Voyage (form 194) I would like the field wpcf-vessel-authorised-reseller in the parent to be used to populate wpcf-voyage-authorised-resellres in the Child Voyage being created by the form.

My php knowledge isn't brilliant, so please do advise if you see any mistakes and / or I'm doing it all wrong!

#579840

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

There was little issue with your code as you did not added correct $post_id with to update postmeta and there is little issue with opening and closing brackets.

Could you please try following code and try to resolve your issue:

// Enter Parent Vessel Authorised Resellers into Voyage fields when New Voyage is Created
add_action('cred_save_data', 'save_vessel_resellers_to_child_voyages',10,2);
function save_vessel_resellers_to_child_voyages($post_id, $form_data){
    // if a specific form
    if ($form_data['id']==194){
        $parent_id = $_GET['_wpcf_belongs_vessel_id'];
		$auth_resellers = get_post_meta($parent_id, 'wpcf-vessel-authorised-reseller', true);
     
		// Enter Child Voyage Post Meta
		update_post_meta($post_id , 'wpcf-voyage-authorised-resellers', $auth_resellers);
    }
}

Also, please make sure that parent Id is available with '_wpcf_belongs_vessel_id' URL param and you are using correct URL param.

If this does not help, I need problem URL where I can see your child post type form.

#579867

Fastest way to do this would be by providing you admin access, because I have multiple files and conditionals set on User Roles etc that restrict and hide the forms, but if you log in as admin you'll have no problem seeing all this.

Do you need to send me a request for me to send these details?

#579868

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I need problem URL where I can see the CRED child form.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#579982

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now. I've added following code to your current theme's functions.php file:

add_action('cred_save_data', 'save_vessel_resellers_to_child_voyages',10,2);
function save_vessel_resellers_to_child_voyages($post_id, $form_data){
    // if a specific form
    if ($form_data['id']==194){

        $parent_id = $form_data['container_id'];
        $auth_resellers = get_post_meta($parent_id, 'wpcf-vessel-authorised-reseller', true);
      
        // Enter Child Voyage Post Meta
        update_post_meta($post_id , 'wpcf-voyage-authorised-resellers', $auth_resellers);
    }
}

Now, I can see it's successfully append the parent field value to child post field value.

#580198

Ok, I see this beginning to work now, though I notice if two resellers are selected for the Parent Vessel, the Voyage will only inherit the first. Is this something to do with the above function not being able to populate repeating fields?

#580297

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - if you have multiple values stored for same key you need to adjust the code as given under:

add_action('cred_save_data', 'save_vessel_resellers_to_child_voyages',10,2);
function save_vessel_resellers_to_child_voyages($post_id, $form_data){
    // if a specific form
    if ($form_data['id']==194){
 
        $parent_id = $form_data['container_id'];
        $auth_resellers = get_post_meta($parent_id, 'wpcf-vessel-authorised-reseller');

        foreach($auth_resellers as $k=>$v):
             // Enter Child Voyage Post Meta
             add_post_meta($post_id , 'wpcf-voyage-authorised-resellers', $v);
        endforeach;
       
        
    }
}
#581175

From what I can tell, using the method above, a child post won't automatically inherit the values of the parent post field if the parent post is edited. In this instance am I correct in saying each individual child post will need to be edited using the form to inherit any changes?

The problem with that is we need any change in the parent to automatically happen in the child without having to manually edit the child post.

To get around this I wonder if there's another possible solution to this issue...

Using php, is it possible to create a function to query the values of the field wpcf-vessel-authorised-reseller in the Vessel (as a parent, grandparent or great-grandparent) and return a value of 'True' if any of the values in the field match the ID of the User currently logged in?

This could then be implemented with a conditional shortcode to only show content if the output is 'True'.

That way the permissions are real-time across all child and grandchild posts whenever they are viewed, as opposed to the user needing to update them in order to inherit permissions.

For reference I already access the fields of grandparent and great-grandparent posts using code such as the example below...

function booking_great_grandparent_shortcode($atts) {
   $booking_id = get_the_ID();
   $variation_id = get_post_meta($booking_id, '_wpcf_belongs_ticket-variation_id', true);
   $ticket_id = get_post_meta($variation_id, '_wpcf_belongs_ticket_id', true);
   $voyage_id = get_post_meta($ticket_id, '_wpcf_belongs_voyage_id', true);
   $voyage_name = get_post_meta($voyage_id, 'wpcf-voyage-name', true);
   $voyage_url = get_permalink($voyage_id);
#581192

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

In this instance am I correct in saying each individual child post will need to be edited using the form to inherit any changes?
==> For now yes or either as you said you need to build such PHP script that will help you to update data for existing parent and childs.

The problem with that is we need any change in the parent to automatically happen in the child without having to manually edit the child post.
==> Well - you need to adjust your code for each action add/edit/delete such that the data you get later on is correct. I just show you a way how you can achieve it. For any post type you want to inherit the data you need to adjust your code accordingly with all add/edit/delete action, if you are using CRED form, then using CRED hook and if you are making changes using admin, then using wordpress standard hook 'save_post'.

See how you can update custom field using PHP - save_post action:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

To understand and fetch parent/child data using PHP:
=> https://toolset.com/documentation/customizing-sites-using-php/displaying-child-posts/
=> https://toolset.com/documentation/customizing-sites-using-php/displaying-child-posts/

Using php, is it possible to create a function to query the values of the field wpcf-vessel-authorised-reseller in the Vessel (as a parent, grandparent or great-grandparent) and return a value of 'True' if any of the values in the field match the ID of the User currently logged in?
==> Yes - you can build your custom shortcode and use with [wpv-conditional] shortcode accordingly.

More info:
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/
[Section: "Checking Custom shortcodes"]

Also, you can use custom functions with [wpv-conditonal] shortcode:
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

#581222

I understand how to do the majority of the above, though is it possible you could let me know how to check field values for a repeating field in php?

#581225

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - when you say "check" - what you wanted to do?

You can fetch repeating field values using following code :

// following line will give you array
$repeating_field_values = get_post_meta($post_id, 'wpcf-your-field');
 
        foreach($repeating_field_values as $k=>$v):
             // do anything
        endforeach;

Where:
- Replace $post_id with your original $post_id and field name.