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'.
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
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 ?
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.
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.
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
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'.
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.
My issue is resolved now. Thank you!