Skip Navigation

[Resolved] How to automatically assign post relationship for new post created by Forms

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Form that creates new posts. I would like to automatically assign that post to a post relationship when the Form is submitted.

Solution: Use the cred_save_data API hook to perform an action programmatically when a post is created by Forms. Use the toolset_connect_posts API to programmatically associate two posts in a post relationship.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 5 years, 3 months 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.

Our next available supporter will start replying to tickets in about 1.38 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by olivierF 5 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1311805

thank you very much christian,

I will check that,

Another question, how can one automatically assign the CPT 'projet created to the CPT 'auteur' corresponding to the user previously created who encoding the project. Knowing that the CPT 'projet' is a child of the CPT 'auteur'.

#1311809

In general, you can use the Post Relationships API to programmatically set post relationships. In your cred_save_data hook, you have access to the new auteur ID, and your wp_insert_post function call will return the new projet post ID. After you have those IDs you can call toolset_connect_posts to connect the auteur and the new projet post. We have documentation for that here:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

#1312301

Hi Christian,
I don't understand how I can do that in my case.
I would like that when the user is connected and encodes a project it is automatically linked to CPT 'auteur' created during the registration with the user form ?

#1312423

To connect two posts, you need the post ID of each post and you need to know the relationship slug. Then you can use the API toolset_connect_posts to connect those posts programmatically.

To get the auteur post ID, assuming there is only one auteur per User, you can use WP_Query or get_posts to query Auteur posts by post author, where the author is the same as the current User. That will give you access to the auteur post ID.
https://developer.wordpress.org/reference/classes/wp_query/#author-parameters
https://codex.wordpress.org/Function_Reference/wp_get_current_user

To get the Project post ID, you would use the cred_save_data hook on the Form where Users create Projects. The post ID is provided in the first parameter, usually $post_id.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
You can remove the parent Auteur select field from the Form, since you will set this value automatically.

Now you have the two post IDs, and you know the relationship slug from wp-admin. So you can use toolset_connect_posts to automatically link these two posts.

#1312463

Thank you Christian,
I'm sorry but I did not understand how to do that.
I do not understand how specifically assigned each project created to the connected user.

You say : 'You can remove the parent Auteur select field from the Form, since you will set this value automatically.'
Where can I do that ?

You say : 'Now you have the two post IDs, and you know the relationship slug from wp-admin. So you can use toolset_connect_posts to automatically link these two posts.'
When i have both id, i create a relationship between the two post but i do not know in advance the id of the project that will be created.

#1312475

I do not understand how specifically assigned each project created to the connected user.
There are two ways to link the User and the Project.
1. Make the User the Project post author.
This is automatic. When the User submits a Form to create a Project, then they are automatically the Project post author.

2. Use post relationships to connect the User's Auteur post and the new Projet post.
To automate this, you must use the toolset_connect_posts API in your Project Post Form's cred_save_data hook to connect these two posts.

You say : 'You can remove the parent Auteur select field from the Form, since you will set this value automatically.'
Where can I do that ?

In the Project Form editor, you can remove the parent Auteur select field. If you used the drag-and-drop Form builder, you can trash that field. If you used expert mode, you must delete the appropriate shortcode from the editor area.

When i have both id, i create a relationship between the two post but i do not know in advance the id of the project that will be created.
Not in advance, but in a cred_save_data hook applied to the new Project Form. You will be able to access the new Project ID when the new Project Form is submitted in the callback as "$post_id".
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1312507
capture.jpg

I'll lean over it.
How can I see in the admin the author of the post (like on the capture).
There is nothing in my CPT 'projet'.

#1312525

It's an option in the post type editor. Go to Toolset > Post Types, and edit the Projet post type. Scroll down to "Sections to display when editing Projet" and check the "Author" checkbox. Then you should see the post author in the Projet dashboard and the Projet editor page.

#1314489

My issue is resolved now. Thank you!