Dear Sir/Madam,
I have two roles "Sales" and "Dealer" and custom post "Dealer", only role Sales can create the post Dealer, role Dealer can only edit the post Dealer. After the Sales created the post Dealer, the post will be assigned the author to the user with role Dealer. How can I create the relationship between the post Dealer and role Sales?
I want to build a view to show all Dealers that are created by the Sales and then the Sales can manage the Dealer posts.
Best regards,
Kelvin.
First, you will need to control with Toolset Access who can do what.
Hence, you will allow the Custom User Role "Sales" to create new Custom Posts of Type "Dealer"
Then, you will allow the Custom User Role "Dealer" to edit existing Custom Posts of Type "Dealer"
This is done in Toolset > Access Control.
https://toolset.com/documentation/user-guides/setting-access-control/
Then, to list the Custom Posts of Type "Dealer" that any "Sales" user, you can create a View, query the User Role "Sales".
Then, create a second View where you query the Post Type "Dealer" and there, add a query filter like:
Post author is set by the parent User View (Select posts with the author set by the parent User View.)
After, insert the second View (where you query Post Type "Dealer") to the first View where you query the User Role "Sales".
Last, insert the View for User Roles into any Page.
This will now list every user of Role "Sales" and below each user, his/her posts of Type "Dealer".
To manage those posts you might add Toolset Forms, to edit the Posts, and make that form only accessible to "Dealers" so they can edit those posts in the front end.
Toolset Forms can be controlled by Toolset Access as well, and simply inserted to the Posts View Loop.
https://toolset.com/documentation/user-guides/access-control-for-cred-forms/
https://toolset.com/documentation/getting-started-with-toolset/publish-content-from-the-front-end/forms-for-creating-content/
I hope this helps!
Dear Beda,
After review your reply, may I know what is the role of the author to the Dealer post at last? Dealer or Sales. I assume the role of the Dealer post author belongs to the user whose role is dealer, Sales is just the creator to create the dealer post for dealer user but the owner of the post should be the dealer but not sales.
May I know at which step I will assign the author of the dealer post to a specific dealer during the post creating?
Best regards,
Kelvin.
I cannot know this, as I am not building this website.
As per your statement, you will have a user Role "Sales" and a user Role "Dealer" - so two of them
As per your opening comment as well, the user with role "Sales" will create the post, hence they will be the authors of that post.
You cannot assign a new author unless you change the author of the Post.
To have a relationship between users and posts where you can have many users assigned to many posts you would need another approach as explained here:
https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/
But this will not change or manipulate the authorship of the user, it will rather mimic the user to a post type, and connect that.
You can update the Post author programmatically if you wish to manipulate the user-author, with a wp_update_post():
$arg = array(
'ID' => $post_id,
'post_author' => $user_id,
);
wp_update_post( $arg );
https://codex.wordpress.org/Function_Reference/wp_update_post
This can be hooked to a Toolset Form with cred_save_data() for example:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Dear Beda,
OK, I added custom field to the custom post and assign the user id to it when creating the post, after creation, I change the author of the post which should belong to. In that case, the post will be owned by the correct author and also have custom field related to the sales.
If you have other better solution, please let me know.
Many thanks for your help.
Best regards,
Kelvin.
It depends on what you want to do with this data.
Of course, changing the Author is the simplest approach but your user doing that also needs access to be able to do so.
It is not possible in the front end Forms without custom code.
The User ID saved in the Field can be a solution to later refer to that user as per ID, but it would require you to either pick (and hence, pre-populate an entire row of users in the select field) or manually insert the User ID, which may not be known by everyone on your system.
As said, to relate users to the same post the probably best approach is the User/posts Relationship elaborated here:
https://toolset.com/documentation/post-relationships/how-to-create-custom-searches-and-relationships-for-users/