Skip Navigation

[Resolved] Split: publishing related many-to-many posts

This support ticket is created 5 years, 9 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 19 replies, has 3 voices.

Last updated by AndreG3332 5 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#1206126

Hi NIgel,

I have figured out that my relationship type was incorrect. I need many to many relationships in this instance.
I have created the right relationship now for the Learner qualification.

This shifted the goal post a bit for me. I tried to set up a relationship form to do the many to many links but am still lost.
My target is to have a page with add new learner take-on form that will include the option to set the learner qualification option.

I have tried to create a relationship form learner qualification but I'm not able to get it working. The setting seems to have me confused a bit.

#1206421

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Front end forms are somewhat lacking in functionality compared to editing posts in the back end, this was a part of the post relationships project that was never fully completed.

With one-to-many relationships you can have a form to publish child posts that includes a selector to connect the parent simultaneously.

But when you move to many-to-many relationships that is no longer possible.

You use normal forms to publish posts (from both the left and right of the relationship) and then you use "Relationship Forms" to connect those existing posts.

So if you are doing this from the front end, that's how you must set up your workflow. Use a form to connect posts after they have already been published.

That is what is described here: https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/

With that in mind, what specifically are you stuck on?

#1206959

That is a curveball I didn't expect. OK so what I would probably want to do now is the following:
Have a learner take on form
Have a qualification take on form
Create a many to many relationship forms to link the student and the qualification.

Then when the student take on has been completed and the facilitator click on update i would like the many to many relationship form to auto open allowing the link between them to be established. However, at this stage, i would like the student field in the relationship form to show only the learner we created in the takeon form that triggered the opening of the relationship form. So effectively i want the leader takeon form to determine the values of the student dropdown field.

Secondly, I would also like to create a filter for the qualification cpt. Let's call it "active" with a yes/no option. and only entries with a yes field must show in the dropdown field for the qualification leg of the student/qualification relationship form.

#1207092

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, you keep on saying "take on form" and I don't understand what you mean.

And could you please be clear about the post types. Learner and student are the same thing, yes?

And we are talking solely about connecting learner posts and qualifications posts?

(Not "learner programs" or "results", which we were talking about before.)

#1207385

HI NIgel,

he takeon form is the term I use for adding a new record for a learner. yes, learner and student are the same. I try to keep my terminology the same but sometimes muddle things up. The learner programs and results is a follow on to the qualification.

I am currently trying to keep it to one topic at a time to prevent further confusion. The chain of events is a learner registers for a qualification. To do the qualification he must do x amount of learner programs which consist of x amount of unit standards, However, the client does not need to grade unit standards but only grade learner programs. I hope this explains it a bit better

#1207652

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, this is a little complex, I had to go through the steps to remind myself as I haven't done it for a while.

The documentation for reference is here (the example of adding a song to a specific album): https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/

First, let's consider that the learner and qualification posts already exists, and our objective is to connect qualification posts to a specific learner post.

What we will do is insert a link on the template to show learner posts which goes to a relationship form where a qualification can be connected. These forms only let you connect one post at a time, so on submission it will redirect back to the learner post, where the same link can be used again to add more qualifications as needed.

So, we have an existing m2m relationship between learner posts and qualification posts.

Go to Toolset > Relationship Forms and create a relationship form to connect existing learner and qualification posts. In the settings make sure it redirects to the post it came from.

Much like edit forms, you need to insert this into a Content Template which acts as a container for the form, so make a new template which is not assigned to anything and insert the relationship form, specifying that it is an "editing mode" template when you do.

If you don't already have a template for displaying learner posts make one now, or edit the existing one, and use the Toolset Forms button to insert a "Connect qualifications to learners link", this time specifying that you are connecting qualifications to a specific learner, the current learner.

Now you should be in a position to test the form.

Visit an existing learner post, you should see the link to connect qualifications, click it and use the form to add a qualification. It should return you to the learner post, where you can repeat this process to connect as many qualifications as you need. (You'll probably want to insert a View here which shows connected qualifications.)

Given this workflow, going backwards a little, if you want it so that an admin creates a learner post using a front-end form and then can immediately start connecting qualifications to that same learner, then all you need is for the form which publishes the learner post to show that post after submitting the form, which will drop us into the workflow we just developed and tested for existing learner posts.

Now, if you find this less than ideal you might want to add your feedback to push for updating normal post forms so that related posts can be connected or added just like in the back end here: https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

That form goes directly to the product manager. Although we have existing requests for this, it will add weight to the request.

Also, your other requirement (only show certain qualification posts according to some custom field) is not currently possible and needs to be a (separate) request.

#1208164

Hi Nigel,
I am going to follow your post on a step by step basis and raise questions as I go along.

"What we will do is insert a link on the template to show learner posts which goes to a relationship form where a qualification can be connected. These forms only let you connect one post at a time, so on submission it will redirect back to the learner post, where the same link can be used again to add more qualifications as needed."

I fully understand this instruction however our first roadblock is as follow:
The client's brief was that on a learner they wanted to first complete a short "BIO" at first and take on all students where after they will want to go back and complete the full bio. The result would be that when short bio take on has been done we will need a different template than what we would need when the full takeon has been done.
My solution to this problem would be to add a page "display short bio added" and add a view to this page. The view will basically be equal to a template of the short bio. On the page, we will then also add the relationship form to link the student and the qualification. Would this be the best approach to bypass the issue of different templates needed for the single post layout between the short bio and the full bio. If this is an acceptable solution how do I then auto-populate the page with the student's bio url to show just the specific student that was just loaded on the page with the view on?

My second question would then be also how do i auto-populate the m2m form that the student details default when the link qualification is opened.

#1208557

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

My preference would be to stick to a single template for the learner.

Are short bio and full bio separate custom fields?

You can use conditional shortcodes to test whether short bio or full bio are empty and display fields (and links) accordingly.

Any changes that are made via edit forms etc. redirect back to show the learner post, which because of the conditional statements would alter accordingly.

Keep everything based around the learner post itself and it will make implementation of things like the forms easier.

The steps I describe above mean that the learner will automatically be pre-selected when adding qualifications via a relationship form.

#1211058

Hi NIgel , No the short and long bios is the same CPT. The one contains only shortened version of full id.

The solution to conditional shortcode sounds like a solution. Do you have documentation about it? I have never used it before.

#1211212

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sure, here is the reference to the shortcode: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional

And here is a fairly lengthy set of documents explaining how to use them in various scenarios: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

#1214167

HI NIgel Thx for the info on conditional outputs. Opened a new world for me.

Still on the topic of relationships. I have now added the "add new learner" form. Now I would like to add one of the relationship fields ie "Qualification" to the cred form. I have formatted the cred form but would like to just add the qualification (many to many relationship with student) field at this stage.

#1214201

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Well, as I mentioned above (https://toolset.com/forums/topic/split-publishing-related-many-to-many-posts/#post-1206421), you cannot include a relationship field in a normal post form for a many-to-many relationship. You can only include a parent field on a form to publish child posts (the one part of a one-to-many relationship).

As soon as you involve many-to-many relationships then you must use relationship forms, which involves connecting already published posts.

That's what I tried to describe in the workflow above: https://toolset.com/forums/topic/split-publishing-related-many-to-many-posts/#post-1207652

And I would once more encourage you to request parity between what's possible in the back end with what is possible using front-end forms, which would make this much easier: https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

#1214528

Thx for your reply. I have submitted an add a feature request for the many to many relationships field to be included in the cred forms'

Ok, I have now created a relationship form between the qualification and the learner. I have added the form to a page called link learner-qualifications. What I would now like to achieve is to open the relationship form page when you update the learner take on. It must then auto-populate the learner name with the learner name form the add learner form so you can just select the qualification. I would also like to "lock" the learner name field to ensure that the user doesn't change the learner by accident.

#1215323

Hi Andre,

Nigel is away and will be back tomorrow, but I'll be happy to follow up.

From your messages, I understand that your original goal is to connect a "qualification" post, to a new "learner" post when it is added through a "add new learner" form.

If that is correct, you can follow a workaround so that you can do that through a single form, bypassing the separate relationship form.

1. In your "add new learner" form, you can add a new generic field of type "select", with a slug "qualification-field":
( ref: https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/ )


[cred_generic_field type='select' field='qualification-field']
{
"options":[add_qualification_field]
}
[/cred_generic_field]

2. That generic field will be set to get values from a custom shortcode "[add_qualification_field]", which will be based on the available qualification posts.

An example of custom shortcode that gets all posts from a particular post type and returns the titles and IDs in JSON format is:


add_shortcode('add_qualification_field', 'add_qualification_field_func');
function add_qualification_field_func() {

	// get all the qualification posts
	$args = array(
		'post_type'        => 'qualification',
		'posts_per_page'   => -1,
		'post_status'      => 'publish',
		);

	$posts_array = get_posts( $args );

	foreach ($posts_array as $post) {
		$data[] = array('value' => $post->ID, 'label' => $post->post_title ); 	 
	}

	// return the them in json format for the generic field
	return(json_encode($data));

}

Note: please make sure to replace "qualification" with the actual slug of your "qualification" post type.

3. The last step would be to make sure that when this "add new learner" form is submitted, selected value from this generic "qualification-field" is used to connect the posts, using the "cred_save_data" hook ( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data ) and "toolset_connect_posts" function ( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts ).

For example, assuming that the ID of the "add new learner" form is "123" and the slug of the relationship is "learner-qualification", where "learner" is a parent and the "qualification" is a child:


add_action('cred_save_data','update_catalog_rel_func',15,2);
function update_catalog_rel_func($post_id, $form_data) {
	if ($form_data['id']==123) {

		toolset_connect_posts( 'learner-qualification', $post_id, $_POST['qualification-field'] );
		
	}
}

Note: Feel free to replace "123", "learner-qualification" and "qualification-field" to match the actual values used on your website.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

For a new question/concern, please open a new ticket.

regards,
Waqar

#1215427

Thx for you reply Waqar.

I would just like to clear up one point with regard to something Nigel told me. He said that you can not do many to many relationships except through the relationship forms. Will that statement have an impact on your approach?