Skip Navigation

[Resolved] Display other child posts on child page with many-to-many relationship

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

Problem: I would like to create a list of links to other child posts on a single child post, when using a M2M relationship.

Solution: In this case, a one-to-many (O2M) relationship is more practical.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 7 replies, has 2 voices.

Last updated by teddyG 5 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#954667
toolsetviewsettings.png

I'm using Toolset for an e-learning portal. I have two main custom post types created with toolset:

1) Trainings - Individual lessons
2) Modules - a container for a group of trainings based on a specific topic.

These 2 post types are related with a many-to-many relationship and an intermediate post type which contains a field to indicate the position of the training in the module. (similar to the Albums and Tracks examples in the docs)

Here's what I'm trying to accomplish:

1) When viewing one of the trainings in a particular module, I need to see the other trainings in this module on the training page (but not the current training being viewed) so users can easily navigate to other trainings in the module.

2) When viewing one of the trainings in a particular module, I need to show previous and next buttons to navigate to the next or previous training in the module. (Note: the intermediate post type has a position custom field that determines the order of the trainings in the module if that's helpful.)

I've tried creating a new view based on the intermediate post type which lists the related trainings but it only displays the same training as the one I'm viewing so I think I need a way to reference the module's post id and pass it to the view (dynamically) so it loads the right trainings?

And have no idea how to handle the sibling post navigation.

See my view settings in the attached screenshot. I've tried multiple variations of this according to the docs but nothing seems to work.

#954709

Hello, having your Modules and Trainings in a M2M relationship may be a problem here. As you described it, when you're viewing a single Training post, there is no way to know which Module is the "parent" because a single Training post can be related to multiple Modules. In order to know which siblings should be shown, you need to know the parent. So from a technical perspective, how is it possible to know the parent? Is it based on the User's navigation path? Is it based on some custom field stored in the User profile? If you have an idea, let me know.

One thing I can think of is to use a View of Training posts, filtered by post relationship, and insert the View on the single Module page. This would help get around the problem where the Module is undeterminable, because it would just be the same as the current page.

#954782

Not sure I understand how the suggestion you made would solve the problem so if you could elaborate that would be helpful.

Issue is displaying the related posts on the Trainings page not the modules page.

What if the trainings template used the intermediary post type to display the training? My assumption is that somewhere in the database the intermediary post type contains both the module post ID and the training post ID so isn't there a way to use the values if the training page content is generated based on the intermediary type instead of the trainings post type?

I can see where this would cause other issues such as all trainings needing to be associated with a module though which isn't currently the case.

#955541

Issue is displaying the related posts on the Trainings page not the modules page.
I understand this. Let me elaborate the problem with an example. Say you have a Training post called "Lesson 1". Lesson 1 is part of Module 1, Module 10, and Module 25. Now let's say I'm visiting the Lesson 1 page on the front-end of the site. How is it possible to know which parent Module I should use to query the sibling posts? Lesson 1 will have different siblings depending on which parent context is used. In Module 1, Lesson 1's siblings may be Lesson 2 and Lesson 3. But in Module 10, Lesson 1's siblings may be Lesson 4 and Lesson 5.

What if the trainings template used the intermediary post type to display the training?
Again, the problem is that it is not possible to know which intermediary post to use for context, because there can be multiple intermediary posts for a single Training.

Not sure I understand how the suggestion you made would solve the problem so if you could elaborate that would be helpful.
My suggestion removes the possibility of having multiple parent Modules for the same Training, so there is no ambiguity about determining sibling Training posts. There is only one set of siblings for each Training post, because there is only one possible parent Module.

#955776

Ok. I get it. So if I change to a 1 module to many trainings relationship, what's the correct way to create this view on the trainings page to show all the other trainings in same module (but not the one they're viewing)?

And is there a best practice to change from many-to-many to one-to-many without breaking things massively?
Right now each lesson is in fact only associated with 1 module.

#956588

So if I change to a 1 module to many trainings relationship, what's the correct way to create this view on the trainings page to show all the other trainings in same module (but not the one they're viewing)?
- Create a View filtered by the Module / Training post relationship, where the parent post is set by one shortcode attribute "wpvrelatedto".
- Check the option "Don't include current page in query results" to filter out the current Training post.
- In the Loop editor of this View, insert a link to the post (this will be a link to each sibling Training post).
- Insert View 1 in the template for a single Training post, and pass in the current post's parent ID in the shortcode attribute "wpvrelatedto", like this:

[wpv-view name="your-sibling-view-slug" wpvrelatedto="[wpv-post-id item='@module-training.parent']"]

Replace "your-sibling-view-slug" with the slug of your View, and replace "module-training" with the slug of the post relationship. You can find that by editing the post relationship in wp-admin. Be sure to include the "@" symbol here. So basically what you're doing here is getting the parent module's post ID, and passing that in to the post relationship filter of the View. More information about passing arguments into Views here: https://toolset.com/documentation/user-guides/passing-arguments-to-views/

#956591

Sorry, I forgot to address your 2nd question.
And is there a best practice to change from many-to-many to one-to-many without breaking things massively?
There's no easy way to migrate those existing relationships, so when you create the new one-to-many relationship you'll have to go through all the Trainings and hook them up to the correct Module. The old many-to-many relationships can be disconnected and deleted, and you can even disable that old M2M relationship so things don't get confusing in wp-admin.

#1072813

Ok. So this was super helpful and I have this working now where it will display the other trainings in the module. Thank you!

Now I'd like to take it one step further...

So the trainings now have a custom number field named "Position in Module" and since now a training can only be connected to 1 module, I'd like to use this value to navigate to sibling trainings in the module.
(or if there's an easier way to navigate siblings open to that as well)

So if the current training I'm viewing is in position 2, I'd like to add a back button that would link to the training in position 1 and a next button that links to the training in position 3.

I imagine this will require 2 views, one for each button with a value passed to it like above.

But how do I create the query that basically says:

For back link:
Find the related training post ID in the parent module where "Position in Module" field value = ("Position in Module" value of Current training being viewed - 1)

And for next link obviously it's +1.

Guessing I need some sort of custom function to generate this value?

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/link-to-next-and-previous-child-posts-on-child-page/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.