Skip Navigation

[Résolu] Custom Field for Relationship using Gravity Forms Post Creation

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
The user is using Gravity form and the "Post Creation" addon to create some custom post type. He would like to link the created post with a parent post that is collected from a Gravity form field.

Solution:
You will need to hook into the gravity form submit process and use the toolset_connect_posts function to link the posts with the Toolset relationship.

The user ended up using this custom code:

// Customer to Cut Sheet Relationship From GF Form Submit
add_filter( 'gform_advancedpostcreation_post_after_creation_4', 'apc_toolset_setparent', 10, 4 );

function apc_toolset_setparent( $post_id, $feed, $entry, $form ) {

toolset_connect_posts( 'customer-cut-order', $entry[21], $post_id );

}
// Customer to Cut Sheet Relationship From GF Form Submit

Relevant Documentation:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
- https://docs.gravityforms.com/gform_advancedpostcreation_post_after_creation/

This support ticket is created Il y a 3 années et 6 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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: Africa/Casablanca (GMT+00:00)

Marqué : 

This topic contains 10 réponses, has 3 voix.

Last updated by simonV-3 Il y a 3 années et 6 mois.

Assisted by: Jamal.

Auteur
Publications
#1788151

Ed

I need to know which custom field (if any) connects a CPT (Cut Sheet) to its parent (Customer), when the post is created with Gravity Forms Post Creation.

it used to be _wpcf_belongs_customer_id, but that doesn't seem to work.

It seems like I've asked this, before.

#1788619

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello and thank you for contacting the Toolset support.

I am not familiar with the Gravity Forms Post Creation plugin or the Cut Sheet and Customer elements. I looked online for any references to no avail.
Can you elaborate more on your questions? What would you try to do?
Are "Customer" and "Cut Sheet" custom post types created with Toolset? Are they connected using a Toolset relationship?

Please provide as many details as possible, so we can understand your request and your website setup quickly and advise you accordingly.

#1788623

Ed

Hey Jamal,
"Customer" is a CPT. "Cut Sheet" is another CPT. "Customers" are single parents of many "Cut Sheets." "Post Creation" is a Gravity Forms add-on. It creates a post (Cut Sheet) when a user submits the form. The "Customer" post ID is passed in the URL that takes a user to the form. I need that "Cut Sheet" to belong to a "Customer."

The form contains a field, "Belongs To Customer," that's automatically populated with the Customer ID, when they get to the form. I need to pass that Customer ID to a custom field, so the new Cut Sheet will be connected with that specific Customer.

This is for a deer processing and taxidermy business. I hope that helps.

#1788697

Ed

This may be related: https://toolset.com/forums/topic/setting-the-relationship-from-form/

It also may not be related.

#1789409

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Thank you, Ed, for these details. I believe the ticket you referred to in your last reply is related to this current one.

Just one more thing to confirm. When you say the posts are linked, parent/child, do you mean by a Toolset One-to-many relationship?
If that's the case, the solution is to use our function toolset_connect_posts that Beda(the supporter) shared in his last reply https://toolset.com/forums/topic/setting-the-relationship-from-form/#post-1129005
Check the documentation of the function here https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

You will need to use this function, either inside a hook from the Gravity Forms. Check this article hidden link

Or inside a "post_save" hook from WordPress.

Because currently, Toolset does not related anymore on a custom field to store the relationship between posts. It is now managed by some extra database tables that Toolset Types manage.

I hope this answers your question. Let me know if you still need any information.

#1789453

Ed

Yes, the relationship is one-to-many. One Customer has many Cut Sheets. Only the business owner will have access to this website. They will be entering all the information, for each Customer who comes in and each Cut Sheet to be created.

As soon as you mention "hook" I'm out. I can place code where it needs to be placed, but beyond that, I'm useless.
For instance, in "add_action( 'gform_advancedpostcreation_post_after_creation_1', 'your_function_name', 10, 4 );" I can guarantee "your_function_name" means more to you, than me.

Is it possible for you to tell me what to add to my functions.php file, to get this working? In my head, it should only be about 5 lines, max. Again, I know nothing.

#1790781

Ed

GF Support suggested I use the following, modified accordingly. I just don't know what to modify it with.

******
add_filter( 'gform_advancedpostcreation_post_after_creation_1', 'apc_toolsset_setparent', 10, 4 );
function apc_toolset_setparent( $post_id, $feed, $entry, $form ) {

// $entry stores your passed values where the ID of the array is the field value
// if you stored your 'parent record' in a hidden field with an id of 7, you'd use
// $entry[7] to retrieve that value. You can use that ID below in the areas where you need it, ie post meta and
// $post_id_of_post_to_relate_to

// Run Toolset Update Post Reference
// I believe $post_id_of_post_where_prf_is_saved can be replaced with $post_id as that is the record just saved
toolset_connect_posts( 'prf_slug', $post_id_of_post_to_relate_to, $post_id_of_post_where_prf_is_saved );

// Replace my_custom_field_key with your custom field meta key.
update_post_meta( $post_id, 'my_custom_field_key', $entry[7] );
}
******

Also, they don't have a license for Toolset, so they weren't exactly sure of the way this needs to be done and I can't believe they don't have a license for Toolset. Maybe you should give them one?

#1791309

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+00:00)

The toolset_connect_posts acepts three arguments:
- The relationship slug, from the Gravity forms support reply, I would say it is "prf_slug", but you will have to go check in the relationship page what is the slug. Check this example hidden link
- The parent post ID. Gravity forms Support has explained that it depends on when you have put it on the form. They suggested the example $entry[7], if it is the 7th field in the form.
- The child's post ID. As the form is for creating Cut Sheet, the post ID is $post_id.
The could should be then:

add_filter( 'gform_advancedpostcreation_post_after_creation_1', 'apc_toolsset_setparent', 10, 4 );
function apc_toolset_setparent( $post_id, $feed, $entry, $form ) {
	toolset_connect_posts( 'prf_slug', $entry[7], $post_id );
}

I am only sure of "$post_id", you will have to check if $entry[7] does hold the Customer ID, and make correct the relationship slug if it is no "prf_slug".

#1791317

Ed

Thank you so much, Jamal!

This is what I ended up using...and it works! 🙂

// Customer to Cut Sheet Relationship From GF Form Submit
add_filter( 'gform_advancedpostcreation_post_after_creation_4', 'apc_toolset_setparent', 10, 4 );

function apc_toolset_setparent( $post_id, $feed, $entry, $form ) {

toolset_connect_posts( 'customer-cut-order', $entry[21], $post_id );

}
// Customer to Cut Sheet Relationship From GF Form Submit

There were some typos in your last response, but they were pretty easy to figure out.

This means EVERYTHING to the project I'm currently working on.

Again, thank you!

#1791319

Ed

My issue is resolved now. Thank you!

#1965075

Dear Jamal

I am having a similar issue and found this topic. I have this filter added:

add_filter( 'gform_advancedpostcreation_post_after_creation_2', 'apc_toolsset_setparent', 10, 4 );
function apc_toolset_setparent( $post_id, $feed, $entry, $form ) {
    toolset_connect_posts( 'muay-thai-gym-review', $entry[8], $post_id );
}

But it isn't working.

I am not sure what I am missing....

The form You find on each custom post like hidden link

Where customers can leave a review (they should be logged in).

Having there 2 hidden fields, one with the "parent" ID.

In toolset my Gym ID from the review custom post type is as single line defined. This should "connect" then the post ID from each Gym. As each Gym are the parent and the reviews the child.

Any tip i can check?

Regards,

Simon

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.