1. A WordPress user can only have one Student post (I don't know how to do it right now, but I'll think about it later).
There is a simple trick elaborated here about this:
https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/
https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/#how-do-i-prevent-users-from-creating-more-than-one-contractor-post
That post will also help you further with the connections of posts/users
2. I need when the user presses the "add to favorites" button:
- create an account (I created the user form to do it)
- create an associated Student post, containing his first name, his name and his email address from his account information
- create the relationship (Favorites) between the Student cpt and the Course cpt
I assume that Button is yet just an Elementor Button, right?
Hence, a prototype of the idea you outline, and not a Relationships form or similar.
In that case, I suggest:
- since you want the button to create an account, the "user" pressing the button is not yet a user, but a "guest". In WordPress, users are logged in, existing users, while guests are visitors without any role on the site.
To create an account in Toolset you use Toolset User Forms. That Button there could maximally lead to a Form that lets the guest fill in some details (user profile) and submit it. Of course, you could insert the Form on that very page and then use the Forms submit button to have the "button" to click.
User forms are explained here, they are quite straight forward to set up:
https://toolset.com/documentation/user-guides/front-end-forms/
You create one in Toolset > User Forms.
- the same button (in this case the User Form submit button) cannot natively create a post as well. That requires Custom Code.
You'd use the Toolset Forms API to apply some WordPress API to the cred_save_data() action. This is fired when the form saves the post (or user) to the database. In that action you can use wp_insert_post() to create a new post with data that for example, you grab from the $_POST of the User Form:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/wp_insert_post/
Example code, to be adjusted:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12)//Change the ID of your form
{
wp_insert_post( array $postarr, bool $wp_error = false )//More details about wp_insert_post() here: https://developer.wordpress.org/reference/functions/wp_insert_post/
}
}
- to create the relationship of that post (I suspect of type "Student") to the "Course" type, you will then (in the same cred_save_data() but after wp_insert_post()), use the Toolset relationships API to insert the relationship.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
Example code to be adapated, an enriched version of above:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12)//Change the ID of your form
{
$my_post = wp_insert_post( array $postarr, bool $wp_error = false )//More details about wp_insert_post() here: https://developer.wordpress.org/reference/functions/wp_insert_post/
toolset_connect_posts( 'Favorites', $my_post->ID, 7 );//where 7 the ID of the course, you could get that from the current post object or elsehow from a Form Field or similar, it depends where that data is stored. "Favorites" is the relationship slug
}
}
It would probably, however, be much easier for you (if you are not comfortable with PHP custom code) to follow a few more steps (not all in the click of one button) and use the Relationships Toolset Forms to connect the posts.
https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/
3. The same is valid for if the user exists already, however here we would ensure with an Access ShortCode or an HTML condition, that existing users do not see the "Create user" form, instead, just a button to connect (and create) posts.
Now, this is a rough overview of how it's done.
I cannot assist you in each sub detail here in the same thread.
Let's use this as a "how to" generic thread, where from you can create subsequent single threads when specific issues/questions come up.
I'd start with the HTML/Access conditions to show/not show data to existing or new users (guests)
Information on that can be found here:
https://toolset.com/documentation/user-guides/access-control-texts-inside-page-content/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
Once you tackle that down, attack the other points and eventually open new single topics for each point so we can help with precise information and more elaborated examples on the Custom Code.
Thanks!