[Resolved] How to bind a user to a custom post type
This thread is resolved. Here is a description of the problem and solution.
Problem:
How can you connect users to posts?
Solution:
The simplest solution is to make the user the author of the post.
In circumstances where that is not possible, e.g. when you need to connect multiple users to a post, you can create a custom post type for the user (e.g. "profile"), make the user the author of that post, and then use post relationships to connect the profile post with the desired posts.
On our website, we are listing the members of our organisation (mainly R&D companies).
We created a custom post type called "Member" which contains several custom fields.
We would like to offer the possibility for a logged-in user to front-end edit its Member profile, ie. a specific post from "Member" type.
For example, when the user logs in, we offer him a link to edit its profile (but not the wordpress user profile, the member profile, which is the company details).
So, I guess, we need to link specific users to a specific Member-posts. The thing is : how can we do that?
Note : we will create the users manually > Nofront-end/public user registration.
The simplest way is to make the user the author of the post.
Note that for custom post types this option is disabled by default, you need to go to Toolset > Post Types and edit the custom post type in question and in the "Sections to display when editing..." section check the author field.
Then for a "My account" page you can create a View to show the post type and add a filter to only show posts where the post author is the currently logged in user.
Otherwise you could use a custom field to store the relationship, either a custom post field to store the user id, or a custom user field to store the post id.
Thanks for your answer.
Solution #1 is indeed the simplest way and could do the job.
But the thing is : what if we need to create several users related to a member (it's gonna be the case for some members)?
Probably, solution #2 could handle the situation but I am not familiar with this trick.
> Could you please provide a link to documentation concerning this method?
> Can the next release of Toolset with many-to-many relationships help in some way to achieve this?
There is nothing specific in the new post relationships that will help, though you can use post relationships.
What you would do it to create a post type for your users, and make the user the author of their "user post".
You can then "connect" multiple users to a post by making the "user posts" child posts of the member post type.
Otherwise, you can store the user ID of a user connected to a member post by making a "user id" custom post field on your member posts. Make it a repeatable field so that multiple users can be added to one member post.
You can then use that field in a Views Query filter to show posts where the linked user id field matches the current logged-in user.
Unlike the post-author filter, you can't compare the field to the current user directly, you must pass it via a shortcode attribute.
So, by way of example, if I have a custom field on the member posts called Linked User then I add a Query Filter to my View as shown in the screenshot, which uses the attribute "linkeduser". I could have called the attribute whatever I wanted.
Then, when I insert the View onto a page using the wpv-view shortcode, I add that attribute and generate the current user id with the wpv-user shortcode, like so: