ChrisM7865
Support threads created in the last 30 days: 0
Favorite Forum Topics
This user has no favorite topics.
Forum Topics Created
Status | Topic | Supporter | Voices | Posts | Freshness |
---|---|---|---|---|---|
Auto-fill fields based on related post
Started by: ChrisM7865
in: Toolset Professional Support
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: |
3 | 9 | 6 years ago | ||
Trying to configure search with relationship CPT fields
Started by: ChrisM7865 in: Toolset Professional Support |
2 | 15 | 6 years ago |