Skip Navigation

[Resolved] Make sorting dependent on relationship

This support ticket is created 5 years, 3 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 5 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1328043

As a relatively new user I can't get my head around the following. What I like to accomplish:

I have a few custom types, let's say Lecture, Video and Image. I have set up mulitple-multiple relationships between lecture<>video and lecture<image>. So each lecture can contain multiple videos and images. Videos and images can occur in multiple lectures. I also have created a template that displays Lecture title, Lecture Content and the related Videos and Images. This is one loop, in which test on the custom type and adapt the layour accordingly. So far, so good.

But now I'd like to create custom sort of the display of the Videos and Images in a specific lecture. Let's say in the order: Image id-A, Video id-D, Image id-B, Video id-C. But for another lecture, containing possibly the same images and videos, I might choose a different sorting.

So I imagine to create a custom field for the relationship, that I can edit while editing a specific lecture. But note that each video and image should have multiple instances of this custom field, depending on the Lecture. But seem not able to accomplish this.

I hope I made myself clear. Is this possible? Is this the right/best way to do it? Any pointers to instructions?

Note that I need this function in the backend only. No need for interactive frontend functionality.

Thanks is advance?

Joost

#1328141

Nigel
Supporter

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

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

Hi Joost

The first thing to consider if we are talking about a custom sort order is that you can only sort something by a property of itself.

If you have a M2M relationship between Lecture <> Videos you can add custom fields to the relationships which are stored in an intermediate post type.

So you could add a numeric field to the relationship so that when you connect a particular video to a particular lecture you can give it an order number of, say, 50. (No nice drag and drop re-ordering, you don't really have any alternative to manually determining the order in this way.)

Connect several videos and/or images to the lecture, each with a chosen value for the custom order field.

You can then create a View to "display" the intermediate posts, and specify the custom field for the ordering setting.

(Note that when creating the relationship you need to specify that the intermediate post type should be visible in the UI to be able to create a View with it; if you didn't, you can go to Toolset > Post Types and edit the custom post type options to make it visible, I think publicly_queryable and/or show_ui are the options you need.)

Now, you don't actually want to display the intermediate posts, of course. If you are starting from your Lecture template then you would need to add a relationship query filter to specify which lecture is the starting point, and then in the output section of the View when you insert fields to be shown using the Fields and Views button use the Post selection tab to specify that you are not outputting values from the current post (the intermediate post) but from a related post (the video or image), which will add the required "item" attribute to the shortcodes that generate the output.

Does that make sense?

#1328603

Hi Nigel,

Thanks for the help. I'm making a few steps, but I'm not there yet. One thing I missed out was to make the intermediate types visible in the WP UI. That's settled. I've copied the debug information. Note that what I call 'lectures' in my question are actually called 'teishos' on the site. Note also that in my question I mentioned two relationships, while actually there are eight custom types related to lectures/teishos.

As next step, I'm unsure how to add the custom field that indicates the order (called 'volgorde in teisho' on the site') as I see two ways to do it. Do I make a global custom field and link that to the relationships. Or do I add a custom field to each relationship one by one? If the latter: does toolset/wp regard that as one and the same for relationships, or are they treated as different variables? Note that I want to have the freedom mix videos and images (and other custom types) and make one ordered view of them, accompanying the lecture. Is that possible indeed? I worry that that's not if they are treated as different variables.

And I'm afraid that I have to edit the ordering custom field in each of the eight relationships seperately. Is that right? Or is there a way to have one single screen for a lecture where I can edit all relationships with that lecture at once?

So far for now. If this is settled, I guess I need some more help with the query filter. But that;s the next step.

Thanks!

Joost

#1329259

Nigel
Supporter

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

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

Screenshot 2019-09-02 at 08.58.28.png

Hi Joost

Because of how the UI for relationships is implemented, you cannot edit the field group for the relationship fields (belonging to the intermediate post types) in the normal way, at Toolset > Custom Fields.

But your different relationships can share the same custom field that you use for ordering.

When you create a M2M relationship and add relationship fields, you have the option to re-use existing fields, which is what you need to do in this case, so that all of the relationships use that same custom order field.

Then when editing a single teishos post you will see the related post metaboxes where you can set the custom order of the connected posts (you can see an example from my own test site in the screenshot).

The View that you then create to query the related posts to some teishos post then becomes a little more complex, because the Content Selection for the View will not be a single intermediate post type, but will be all 8 intermediate post types that are connected in relationships to a teishos post.

Assuming you will be displaying this View in a template for single teishos posts, you will add a post relationship Query Filter "Select posts in Any relationship that are related to the Post where this View is shown".

And you will specify your custom order field in the orderby setting.

So that will return a mix of intermediate posts from the connections of teishos posts to videos, images etc. amongst the 8 relationships.

In the output section, if you were to simply output the post title it would be outputting the title of the intermediate posts for each connection between, for example, a teishos post and a video post, which is automatically generated, but from which it should be clear, if you want to test at this stage, that it should be working correctly.

Then the final step is to output the fields from the related posts rather than the intermediate posts.

If you had just one relationship this would be fairly straightforward. When you use the Fields and Views button to insert a field (e.g. the post title) you can use the post selection tab to specify that the source for the field should be a related post rather than the current post, which adds an item attribute to the shortcode for the field that specifies where the alternate source is (e.g. item="@relationship-slug.child").

It is necessary to specify the relationship here. Your problem is that it could be one of 8 relationships.

Here's how I would organise this, otherwise it could get unmanageable.

I would create 8 content templates, one for the intended output for each connected post type. Don't assign the template to any post type. For the video post type, if you are inserting the title, specify that the source should be the related post from the teishos-video relationship. For the image post type specify that the source should be the related post from the teishos-image relationship, etc.

Now in the Loop Output section of your View you will need to insert all 8 of these templates, each one wrapped in a conditional shortcode that uses the wpv-post-type shortcode to test the post type of the current post in the loop. If the post type is the intermediate post type of the teishos-video relationship, insert the template for related videos. If the post type is the intermediate post type of the teishos-image relationship, insert the template for related images, etc.

It is somewhat cumbersome to set up, but is necessary for what you are aiming for.

If you get stuck, let me know.

#1335447

Hi Nigel,

Thanks for the elaborate help. The good part of it is that it confirms it is complicated and that I didn't miss something trivial. However, I believe I should let it rest for now. It's a very nice feature and I'm sture I'll give it priority at some point. But for now it appears too complicated to justify all the work needed get it properly implemented and tested.

No doubt I'll revert back to your input at some point.

Thanks very much, I really appreciate the quality of your support!!

Joost

#1336279

Nigel
Supporter

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

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

You're welcome 🙂