Skip Navigation

[Resolved] Saving a relationship when creating a post via Gravity Forms + Custom Post Type

This support ticket is created 7 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 7 years ago.

Assisted by: Luo Yang.

Author
Posts
#589269

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

#589337

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

#589600

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.

#589657

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/