I am creating a custom post (Project) using a Gravity Form. I am using the Gravity Forms + Custom Post Type plugin to save the form data as a post.
I'm having trouble figuring out how to create a relationship between the Project CPT and another CPT called Client.
I found this old thread:
https://toolset.com/forums/topic/how-to-set-the-parent-using-gravity-forms/
but there doesn't appear to be a postmeta field for "belongs". Has this process changed?
I have a dropdown field in my form that lists all the clients. When I select one, i would like that client to be set as the post's parent.
I'm hoping you might have an idea how this can be done.
Thanks,
David
Dear David,
In the Types plugin side, the process has not changed, the custom field slug is still the "_wpcf_belongs_[client-cpt-slug]_id", if the post type slug is "client", then the custom field slug is "_wpcf_belongs_client_id".
How do you setup the dropdown field that lists all the clients?
if it is passing the value of "Client" post title, you will need to get the "Client" post first, for example, with wordpress function get_page_by_title():
https://codex.wordpress.org/Function_Reference/get_page_by_title
Here's how I'm building the dropdown list of clients:
//Populate Clients Dropdown in Projects
function winwar_populate_dropdown_with_posts($form){
foreach($form['fields'] as &$field){
if($field['inputType'] != 'select' || strpos($field['cssClass'], 'populate-with-posts') === false)
continue;
$posts = get_posts('numberposts=-1&post_status=publish&post_type=client');
// update 'Select a Client' to whatever you'd like the instructive option to be
$choices = array(array('text' => 'Select a Client', 'value' => ' '));
foreach($posts as $post){
$choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
// $choices[] = array('text' => $post->post_title, 'value' => $post->ID);
}
$field['choices'] = $choices;
}
return $form;
} add_filter('gform_pre_render', 'winwar_populate_dropdown_with_posts');
Then for the relationship, I'd done this:
add_action( 'gform_entry_created', 'save_types_relationship' );
function save_types_relationship( $entry, $form ) {
if($form['id'] == 1){
gform_update_meta( $entry['id'], '_wpcf_belongs_client_id', $_GET['parent_id'] );
}
}
But that's not working.
I checked your PHP codes, seems you are using post title:
$choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
// $choices[] = array('text' => $post->post_title, 'value' => $post->ID);
You can simply change it to post ID:
// $choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
$choices[] = array('text' => $post->post_title, 'value' => $post->ID);
Then update the "_wpcf_belongs_client_id" with the post ID value.
Since it is a custom PHP codes problem, if you still need assistance for it, please provide a test site with same problem, fill below private detail box with login details and ftp access, also point out the problem page URL and where I can edit your custom PHP codes.
You can also check our Contractors for it:
https://toolset.com/contractors/