Hello, I'm having difficulty creating a parent/child post relationship with Gravity Forms and Gravity Forms Custom Post Types plugin. I've been sitting with this now for almost two days and have read all the related posts on the forum, but for some reason, I just can't make it work.
I have a parent CPT 'Stations' and a child CPT 'Team Members'. I have a Gravity Form with ID 3 to create a new post in 'Team Members'. I want to create a relationship between this new Team Member and one of four stations, which have Post Id's 5324, 5375, 5383, 5384.
Right now I'm not even concerned with dynamically getting the parent ID yet (that's the next step) because my testing with a default parent ID is not even working:
// Attach Team Member CPT to Parent Station GForms "Add Team Member"
add_action("gform_after_submission_3", "update_team_member_station_data", 10, 2);
function update_team_member_station_data($entry) {
update_post_meta($entry['post_id'], '_wpcf_belongs_stations_id', '5383');
}
It's not creating the relationship. What else do I need here? Or am I missing something in the above code?
Thank you in advance!
Hello,
In the latest version of Types plugin, please try the new API function toolset_connect_posts() to connect the posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
Excellent, thank you!
This works:
// Attach Team Member CPT to Parent Station GForms "Add Team Member"
add_action("gform_after_submission_3", "update_team_member_station_data", 10, 2);
function update_team_member_station_data($entry) {
toolset_connect_posts( 'station-team-member', $entry[39], $entry['post_id'] );
}
I do have a question though with regards to the Gravity Forms field ID 39: it is a custom field of drop-down select type, and I still have the custom field value set with ‘_wpcf_belongs_stations_id’ (see screenshot) -- is this still necessary or will a normal drop-down field (no custom field) work with values set as post ID’s?
Thanks for your help!
In the latest version of Types plugin, the "_wpcf_belongs_[parent-slug]_id" custom field is not needed,you might need to:
1) In Types plugin side, use function toolset_connect_posts() to connect the posts
2) In Gravity Forms side, I suggest you check it with Gravity Forms support:
hidden link
check if there is any API that can generate the custom select field:
- You can get all parent posts with WordPress function, and populate them as options of the select field:
https://codex.wordpress.org/Template_Tags/get_posts
- Get the related parent "station" post with function toolset_get_related_post(), use it as default option of the select field:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
Feel free to create new thread if you have any new question, that will help other users to find the answers.