Skip Navigation

[Resolved] Set a post as a child

This thread is resolved. Here is a description of the problem and solution.

Problem: I have custom post types Enquires and Team Members. I would like my Guest users to be able to use CRED to create a new Enquires post and select one or more Team Members to associate with the Enquire post. Team Members can be associated with more than one Enquire. What type of post relationship should be used here?

Solution: Many-to-many relationships are designed to allow one or more type A posts to be related to one or more type B posts. These relationships are fairly easy to manage in wp-admin, but they are not as straightforward to manage in CRED forms. Some custom code may be required to create a generic multiselect or checkboxes field that captures a User's selections and applies them on the backend. You can use the cred_save_data hook to accomplish this.

Relevant Documentation: https://toolset.com/documentation/user-guides/many-to-many-post-relationship/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms
https://developer.wordpress.org/reference/functions/wp_insert_post/

This support ticket is created 7 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by PiotrO586 7 years ago.

Assisted by: Christian Cox.

Author
Posts
#582376

Hi, I am considering the following scenario:

I have Team Member and Enquires custom posts. I set Enquires a parent to Team, because one Enquiry may be related to many Team Members.

A user filling an Enquiry Form (the user is not logged) should be able to choose whether to attach one or more Team Members to the enquiry or to attach none. The same should be possible for a manager – to assign one or many Team Members to (or unlink from) the enquiry. So the point is to be able to set (or detach) a child (children) to the parent.

How to achieve this workflow with Toolset?

Regards,

#582515

Hi, I assume that Team Members can be associated with multiple Enquiries, correct? So it sounds like what you really need here is a many-to-many relationship, because you need to be able to associate many different Team Members with a single Enquiry, and you need to be able to associate each Team Member with many different Enquiries. This is what many-to-many relationships are designed to accomplish.

We have a guide that talks about many-to-many post relationships here, and we are working towards updating this functionality:
https://toolset.com/documentation/user-guides/many-to-many-post-relationship/

This type of relationship is fairly easily managed in the wp-admin area, but it's not currently integrated in our front-end post management system, CRED. Your not-logged-in users would use CRED to create Enquiries. If your Managers have access to wp-admin, then they should have no problem managing the relationships between Team Members and Enquiries. But for the front-end of the site, custom code would be required to allow Users to select a Team Member or Members, then capture those selections and use them to define relationships with the new Enquiry on the back-end. We offer a PHP API that will allow you to hook into the form submission process, so you could add your own PHP code that performs those tasks:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Let me know if you have additional questions about this, and I'll be glad to take another look.

#582657

1. So, sticking with my example, does it matter if I set Enquiries as parent of Team Members or Team Members as parent of Enquires?
2. “But for the front-end of the site, custom code would be required to allow Users to select a Team Member or Members” – do you offer any code samples how to set such relationship? Which elements are required to make a relationship?
3. Do you plan to support many-to-many relationships in CRED?

#583083

1. So, sticking with my example, does it matter if I set Enquiries as parent of Team Members or Team Members as parent of Enquires?
Not really, because neither will work in your case. Let me explain why.
Assume User 1 wants to enquire about members John Smith and Robert Jones.
Assume User 2 wants to enquire about members Robert Jones and William Brown.
If you make Enquiries parents of Team Members, then Robert Jones cannot be a child of more than one Enquiry. It's not allowed to have more than one parent within a single post type.
If you make Team Members parents of Enquiries, then the first Enquiry cannot be a child of more than one Team Member. Again, it's not allowed to have multiple parents within a single post type.

2. “But for the front-end of the site, custom code would be required to allow Users to select a Team Member or Members” – do you offer any code samples how to set such relationship? Which elements are required to make a relationship?
There are several steps here, and the end result is a custom set of checkboxes or a multi-select field that allows you to capture the Member IDs you want to associate with each Enquiry. You can create this input by using a View of Team Members that outputs raw data for the options of a generic checkboxes field.
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms

Then you can access these selections using the cred_save_data hook and the $_POST superglobal:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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)
    {
        if (isset($_POST['my_custom_field']))
        {
            // create your intermediary posts here using wp_insert_post, see below
        }
    }
}

https://developer.wordpress.org/reference/functions/wp_insert_post/

A many-to-many relationship is established by creating an intermediary post that is a child of both post types, i.e. the intermediary post is a child of both an Enquiry and a Team Member. Parent / child relationships are established in the database using a hidden postmeta field with the syntax "_wpcf_belongs_{slug}_id".

$my_post = array( 
  'post_type' => 'intermediary-post-type-slug',
  'post_status' => 'publish',
);
wp_insert_post($my_post);

After you create the intermediary posts using wp_insert_post, you would have to call add_post_meta to add the parent references for each parent post type:

$enquiry_id = // probably = $post_id if this code is part of your cred_save_data hook for creating an enquiry
$team_member_id = // one of the selected team members' ids. Access this from the $_POST superglobal.
add_post_meta($post_id, "_wpcf_belongs_enquiry_id", $enquiry_id);
add_post_meta($post_id, "_wpcf_belongs_team-member_id", $team_member_id);

Code above assumes your CPT slugs are "enquiry" and "team-member", but yours may vary.

As you can see, this is a detailed process that probably warrants a separate ticket for more in-depth support.

3. Do you plan to support many-to-many relationships in CRED?
Yes, as the new changes to many-to-many relationships are rolled out, CRED will be updated to support them on the front-end. However, I do not have a timeline available for those new features just yet.

#583093

Thank you for the hints. As I am new to Toolset I try to find some optimal workflow/structure before developing a site.

One more thing in the thread: I am wondering if setting up CTP like Team Members is reasonable, if I may put some additional fields to the User and rely on post author to group a post with an author in a view or archive, right? It's something like parent–child relationship between author and post. I can even show users in front-end with a view. BUT, may I allow someone to manage users through front-end or attach taxonomy to users?

#583346

BUT, may I allow someone to manage users through front-end or attach taxonomy to users?
Yes and no. CRED can be used to create or edit existing Users on the front-end of the site. You could use CRED to create a registration form where Users can sign up to create an account. You can also create a CRED form that edits existing Users. Either of those will work on the front-end of the site. However, there are 2 limitations you should be aware of:

1. WordPress does not support User Taxonomies by default. Instead, you could use custom fields to establish relationships between Users. Or, you could write your own custom code that sets up custom User taxonomies as in this article:
hidden link

2. It is not currently possible to delete Users with CRED. Instead, you could create a custom User role using Toolset Access or a custom field using Types to effectively "deactivate" a User with CRED.

#583413

Thanks for the hints. That helped me a lot.