Skip Navigation

[Resolved] Multiple post types in relationship to one post type

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

Problem: I have a Form that edits a parent post type. This parent post type has two different child post types. I would like to show a single select field containing all the child posts from both post types, and allow Users to select one parent and one child to associate with one another.

Solution: There's nothing exactly like this built in to Forms. You might be able to achieve something similar with custom code. Use a View to populate a generic select field with parent posts. Use another View to populate a generic select field with child posts from both post types. Then use the PHP APIs to link the two posts programmatically.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This support ticket is created 5 years, 4 months 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 15 replies, has 2 voices.

Last updated by grahamA 5 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1305463

Tell us what you are trying to do?
I have a one to many relationship but i need to allow 2 post types to connect to my one post type but the radio buttons only allow one selection on the 'Many' option
Is there any documentation that you are following?

Is there a similar example that we can see?
ie. I have a post from post type A and wish to associate it with many posts from post type B+C. Is this possible?
What is the link to your site?

#1305649

Hi, yes it's possible but you must create two one-to-many (O2M) relationships:
O2M relationship 1: CPT A > CPT B
O2M relationship 2: CPT A > CPT C

#1305679

Thanks, yes I had tried that but unfortunately I need to provide my user a relationship form to search for their chosen post from type A and that means displaying two of those too which looks clumsy as a gui. So, alternatively is there a way to bring both relationships into one relationship form or any other solution please?

#1305937

A post relationship Form isn't actually required for managing O2M relationships, that can be done in the Create Post or Edit Post Form for the child post type. When you create a Form to create post type A or edit post type A, both parent post type select fields will be automatically included in one single Form. So if you need to allow your Users to search for some post type A, then you could do that with a custom search View. In the results of that View, you could include a link to edit that post with Forms. Place the Edit Post Form in an unassigned Content Template so you can select it when you choose to insert the link to edit the post. Is this workflow viable for your case?

#1307975

I don't think that quite covers what i am trying to achieve.

I have a post type of 'Small Products' which creates posts of SP1, SP2, SP3
I have a post type of 'Big Products' which creates posts of BP1, BP2, BP3
I have a post type of Clients which creates posts of ClientA, ClientB, ClientC

I wish to have my user search both all Small Product posts and all Big Products by title from one drop down list, select their chosen post and attribute it to a client post of their choosing.

How can I achieve this or part of it within toolset functionality.

Thanks

#1308879

There's nothing exactly like that in post Forms or relationship Forms. Generally Forms are limited to a single post type, so there's not a single field that allows you to pick from more than one post type. If you tried to edit a Big Product post in a Small Product Form, that Big Product would be converted into a Small Product unless you used some custom code to prevent that.

Similarly, Relationship Forms are designed for a specific child post type and a specific parent post type, so there's no easy way to allow Users to search from two post types in a single input.

So you would have a couple of options here. You could use a custom search View that allows Users to search for Small and Big Product posts, and produces a list of both Products. In that list, you could include a link to an edit post Form where the User could select a Client parent post. So the search would happen outside the Form.

The other option is to use a generic select field, populated with a View of both child post types, in an edit post Form. That Form would include a Client select field. Then you would have to use custom code to ensure the child post types are maintained accurately, since an edit post Form is only able to handle one post type by design.

Generic select field using a View:
https://toolset.com/forums/topic/select-type-field/#post-1204956

Form API to help maintain child post types:
https://toolset.com/documentation/programmer-reference/cred-api/

#1310267
form with view.jpg

Hi.
I have followed your second option with a view shortcode in an edit form but I am not sure I am seeing what I had hoped. Please see attached image.
I can see all my posts from two types(small products and big products) but when I click submit nothing happens. I would have hoped it would have opened the relevant post in its own type. The bit that is confusing me is that in the edit form I have to choose my other post type to edit (Clients) just to get the form to render/appear.
At this stage I am just need ot prove the operation so not worried about custom code, but so far nothing will open.

Any thoughts?

Thanks

#1310497

Is it possible for me to log in and see how you have this set up? Please provide login credentials in the private reply fields here and let me know how I can see the Form on the front-end of your site.

#1310517

I will need to setup a new site and configure it all for you. This will take a few days as I am juggling projects at the moment so can you keep this ticket open please?

#1310521

Yes of course, I'll stand by for your update.

#1315457

I have the test site ready for you but how do I send you the login information privately please?

#1315573

I have activated private fields here so you can share a login confidentially. Don't forget to let me know where I can see the Form on the front-end of the site.

#1316371

Okay it turns out I was mistaken - the system will not allow you to edit post type A in a Form designated for post type B. So for #1, you'll have to create two Forms and place them in two unassigned Layouts. I can see from this code in the Search View:

<td>[wpv-post-title] [toolset-edit-post-link layout_slug='small-layout' target='self']Edit %%POST_TITLE%%[/toolset-edit-post-link]</td>

...the "small-layout" Layout was selected to contain the edit post Form. However, that Layout was in the trash, and also had no Edit Post Form cell or shortcode. So I restored the Layout from the trash and placed the edit bp form in that Layout. Now you can see the edit post link shortcode works for those BP posts in the Search View. If you'd like to use a different Layout, replace the layout_slug with another Layout slug.

To display Edit Post Links for another post type in the same View, you'll have to create another Template Layout and place the Edit Post Form for that post type in a Forms cell in that template Layout. Then in the View, insert another edit post link shortcode with that Layout selected. You can add conditional HTML to display the correct edit post link depending on the post type, shortcode [wpv-post-type].

For #2, a few things I need to point out.
- The View must not be paginated. You must return all the products at once for the select field options.
- The Loop output must use this exact template:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-item index=1]{"value":"[wpv-post-id]","label":"[wpv-post-title]"}[wpv-item index=other],{"value":"[wpv-post-id]","label":"[wpv-post-title]"}</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-layout-end]

There should be no line breaks or spaces after the code. You should also check the checkbox "disable wrapping div around the View" in the View editor.
Let me know the results and we can go from there.

#1316515

Thanks Christian.

WOW. Option one is all working and I understand what is going on but unfortunately the user experience is not as good as Option 2.

I have option 2 giving me both the product list and the client list, the only bit I cannot see how to do is when the submit button is pressed it actually applies the relevant relationship.
On the site go to the 'assign to client' page in the menu when logged in as 'tom' pw: 'developer' and you will see what I mean.

BTW. If I use up my support time is there an option to buy extra on projects like this?

Thanks

#1316595

I have option 2 giving me both the product list and the client list, the only bit I cannot see how to do is when the submit button is pressed it actually applies the relevant relationship.
Okay that part isn't automatic. It requires a bit of PHP scripting using two of our APIs: toolset_connect_posts and cred_save_data. The general idea is you add one cred_save_data hook specifically configured for this form, and include a toolset_connect_posts call to link the two posts programmatically.

Part 1 is setting up a cred_save_data hook. Here's a template for that code:

add_action('cred_save_data', 'auto_connect_from_generic_select',10,2);
function auto_connect_from_generic_select($post_id, $form_data) {
  $forms = array( 12345 );
  if ( in_array( $form_data['id'], $forms ) )
  {
  // this is where you add more code
  }
}

Replace 12345 with the numeric ID of your edit Client Form (you can find that in the Forms list in wp-admin). You could change the name of the function in both places if you'd like to use something else. This code will go in your child theme's functions.php file, or you can create a new custom code snippet in Toolset > Settings > Custom Code. More information about this API: https://toolset.com/documentation/programmer-reference/cred-api/

The second part is to call toolset_connect_posts to link the two posts. You can check the documentation for this API here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
In order to connect two posts you need to know both post IDs and the post relationship slug. The ID of the post selected in the generic field will be the value of the generic select field. You can access that in the $_POST superglobal. So if your generic select field has the slug "my-generic-select", you could get the selected value like this:

$selected_id = $_POST['my-generic-select'];

Since the Form edits a Client post, you will have access to the Client ID in the variable $post_id. So you have the two post IDs. Next you need to know which post relationship you want to create. Since you can have different post types in the generic select field, the post relationship slug will also be different depending on the selected post. So first, you need to get the post ID of the selected post. Use get_post_type: https://developer.wordpress.org/reference/functions/get_post_type/

$post_type = get_post_type( $selected_id );

Use that $post_type information in a PHP conditional to determine the correct relationship slug, and call toolset_connect_posts with the 3 required parameters in order - relationship slug, parent id, child id.

Let me know if you need more information about that.