Skip Navigation

[Resolved] Relationship between custom type and users

This support ticket is created 7 years, 1 month 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.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Author
Posts
#590681

I have seen similar queries on how to solve this, but I did not find precisely this case in particular.

I have a custom type called "workspaces" that I have to relate to users. A "workspace" can be assigned many members (users) and a user can be a member of many "workspaces".
Because you can not establish custom type relationships with users directly, I thought to do the following:

Create the custom type "workspaces" and another one called "members" with the custom fields ("ID", "name" and "last name") and create a parent / child relationship where "workspace" is a parent and "members" is son.

When creating a new user with CRED from the front end, use the "cred_save_data" hook to also save the "ID", "name" and "surname" of the user in the custom type "members", in order to assign many members to workspaces.
Then, when you create a new workspace, you can assign the members you want from a list and when you enter to see a workspace, get the related members through the "ID" field, with "ID" of users.

So that in this way, you can maintain the relationships between workspaces and members (users), and then manipulate the views.

This is a good way to solve it?
If so, what style would have the function to be able to save the 3 user fields in the fields of the custom type members? Keep in mind that the fields belonging to members must also be updated when a user is edited (first and last name) and when it is deleted.

Thanks in advance.

#590785

Yes, this is a good way to achieve it.
Keep in mind that if you want a many to many relationship (many users can belong to many workspaces, and vs), you cannot yet do that with Toolset.
You would need an Intermediary Post Type that connects your Data. But that is a detail of organization and display, it does not affect how you update your Posts with User Data.

To Update a Post with User data, you use the cred_save_data() function.
You get the data from the $_POST variable of the CRED Form.
Then you use wp_insert_post() to insert a new post with this data through the cred_save_data() action.
https://developer.wordpress.org/reference/functions/wp_insert_post/

After you can run a update_post_meta() on this new Post so to pass the new user data to the fields
https://codex.wordpress.org/Function_Reference/wp_update_post

For the Edit part you just apply a new cred_save_data() that does NOT have a wp_insert_post() (since the post exists already) but only the needed update_post_meta().

Here you can see a working example of such a code:
https://pastebin.com/2BTbVbcs