Skip Navigation

[Resolved] Auto-fill fields based on related post

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

Problem: I would like to copy the values from some custom fields in a parent post into a child post whenever these two posts are associated in wp-admin.

Solution: We offer a post relationship API called toolset_association_created that will allow you to make updates whenever two posts are associated in the backend of the site. Here's an example showing how to get the value of a custom field with the slug "example" from the parent post and apply it to a custom field with the slug "copied-example" in the child post.

add_action( 'toolset_association_created', 'toolset_update_child_post_meta', 10, 5 );
function toolset_update_child_post_meta( $relationship_slug, $parent_id, $child_id, $intermediary_id, $association_uid ) {
  if( get_post_type($parent_id)=='parent-post-type-slug' && get_post_type($child_id)=='child-post-type-slug' ) {
    $ex_field = get_post_meta($parent_id, 'wpcf-example', true);
    update_post_meta($child_id, 'wpcf-copied-example', $ex_field);
  }
}

When you edit the child post in wp-admin, you will disconnect the original parent post and select a different parent post. The field will be updated automatically in the database BUT it won't appear in the child post editor until you refresh the page. This means if you submit some other change in the child post after selecting a new parent, but before you refresh the page, the copied field will be overwritten with whatever value was present before you changed the parent post.

If you edit the parent post in wp-admin and make a change to the custom field value, it will not be automatically propagated to the child posts.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created

This support ticket is created 6 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 3 voices.

Last updated by Monika Beck 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#1147438

As an alternative, if in the CPT, a new/different floorplan is selected, is there a way to auto-fill certain fields from that new parent relationship?

Example:
Unit 1300 is created (or updated) and floorplan Simple is selected. Simple has 3 bedrooms. Is it possible for Unit 1300 to have a field: bedrooms which will be auto-filled with the bedrooms (3) from the parent floorplan Simple?

Thanks.

#1147439

I'm not really following, can you walk me through this in more detail?

#1147447

Basically, I don't want the client to have to re-enter data twice, or to have to update data when they make a change (uncommon) to the parent.

If the CPT was able to pull the sortable fields from the parent, I could filter based on those fields which are within the CPT without workarounds. This eliminates data discrepancies. If the parent floorplan has 3 bedrooms, the Unit would also have 3 bedrooms as soon as the floorplan is selected, but it would be in a field within the Unit.

I hope that makes more sense.

Thanks.

#1149273

There's nothing in Toolset that will automate this for you, but it might be possible with custom code. Do you plan to update these child posts on the front-end of the site with Forms, or in wp-admin?

#1149274

Hi Christian,

In this instance, only in WP-Admin.

#1149375

Okay I can help a little bit with this, but it's not a perfect solution. We offer a post relationship API called toolset_association_created that will allow you to make updates whenever two posts are associated in the backend of the site. Here's an example showing how to get the value of a custom field with the slug "example" from the parent post and apply it to a custom field with the slug "copied-example" in the child post.

add_action( 'toolset_association_created', 'toolset_update_child_post_meta', 10, 5 );
function toolset_update_child_post_meta( $relationship_slug, $parent_id, $child_id, $intermediary_id, $association_uid ) {
  if( get_post_type($parent_id)=='parent-post-type-slug' && get_post_type($child_id)=='child-post-type-slug' ) {
    $ex_field = get_post_meta($parent_id, 'wpcf-example', true);
    update_post_meta($child_id, 'wpcf-copied-example', $ex_field);
  }
}

When you edit the child post in wp-admin, you will disconnect the original parent post and select a different parent post. The field will be updated automatically in the database BUT it won't appear in the child post editor until you refresh the page. This means if you submit some other change in the child post after selecting a new parent, but before you refresh the page, the copied field will be overwritten with whatever value was present before you changed the parent post.

If you edit the parent post in wp-admin and make a change to the custom field value, it will not be automatically propagated to the child posts.

#1149381

Hi Christian,

This is good, I think this might work for what I need. I don't see the parent needing to be updated as long as they're set up correctly initially.

Thank you 🙂

#1150004

Great, let me know if you run into problems setting this up.

#1160926

Hi Christian,

I am trying to do something similar but can't get it to work. Could you please look at https://toolset.com/forums/topic/add-new-relationship-item-does-not-work-as-expected/?
I have tried the toolset_association_created hook, but the update_post_meta() does not do anything...

Thank you!
Monika