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 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.
This topic is split from https://toolset.com/forums/topic/trying-to-configure-search-with-relationship-cpt-fields/
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 5 years, 11 months ago.
Assisted by: Christian Cox.