Skip Navigation

[Resolved] Sort view by custom field in relationship

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

Problem: I have two custom post types in a M2M relationship, Persons (parent) and Project (child). I have added a custom field to the relationship, and I would like to sort a View of related Project posts using the custom field value. I would like to place that View of related Projects inside another View of Persons, to create a list of all Persons and their related Projects.

Solution:
- Make sure the intermediary post type is set to be visible. Go to Toolset > Post Types and edit the intermediary post type. Set the status to "Publish" and check the Options panel to be sure publicly_queryable, show_ui and show_in_menu are checked.
- Create a View of Projects. In the Loop Output area, insert a shortcode to display the post title.
- Create a View of Persons Projects (the intermediary post type). Set up a post relationship filter. Choose the correct M2M relationship and "related to the current post in the loop". Sort by the custom field value as needed.
- In the Loop Output area of the View insert a shortcode to display the custom field role information.
- Just after the role information, insert another post title shortcode. This time use the Post Selection tab in the popup to select the related Person post as the source of the title. This will display the related Person's name.
- Now insert the View of Persons Projects inside the Loop of the View of Projects, and insert the View of Projects somewhere to test it out on your site.

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

Last updated by deanL 5 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1078480

This is what I have done so far:

Created two custom post types
1. Persons
2. Projects

Connect both CPT with many-to-many relationship:
Persons [*] << Persons Projects >> Projects [*]

Each person may involve in more than one project and hold different role.
So I created a custom field [Single line] under the "Persons Projects" relationship called "Project Role".

Consider the following scenario

Alpha is involved in project X-ray (as General) and Yankee (as Lt General)
Bravo is involved in project X-ray (as Lt General) and Zulu (as General)
Charlie is involved in project Yankee (as General) and Zulu (as Lt General)

To make it easy for you to understand, this is the structure:

Persons
- Alpha
- Bravo
- Charlie

Projects
- X-ray
- Yankee
- Zulu

Relationships
- Alpha - X-ray - General
- Alpha - Yankee - Lt General
- Bravo - Zulu - General
- Bravo - X-ray - Lt General
- Charlie - Yankee - General
- Charlie - Zulu - Lt General

I managed to get this far. Now I'm stuck with the following. I need to present the data in the following structure:

Project X-ray
- General
- - Alpha
- Lt General
- - Bravo

Project Yankee
- General
- - Charlie
- Lt General
- - Alpha

Project Zulu
- General
- - Bravo
- Lt General
- - Charlie

The challenge I'm facing is: I can't figure out how to sort items by "Project Role" which is the custom field of the relationship.

I hope I have explained this well enough.

#1078600

Hi, yes I think I understand what you want to accomplish. It looks like you need to display the role custom field information as well as sort by it. Unfortunately it is not possible to sort a View of some post type using the custom field values in related posts. The best way I can think of to approach this is to use two nested Views - one of the Project posts and another of the intermediary post type.

- Make sure the intermediary post type is set to be visible. Go to Toolset > Post Types and edit the intermediary post type. Set the status to "Publish" and check the Options panel to be sure publicly_queryable, show_ui and show_in_menu are checked.
- Create a View of Projects. In the Loop Output area, insert a shortcode to display the post title.
- Create a View of Persons Projects (the intermediary post type). Set up a post relationship filter. Choose the correct M2M relationship and "related to the current post in the loop". Sort by the custom field value as needed.
- In the Loop Output area of the View insert a shortcode to display the custom field role information.
- Just after the role information, insert another post title shortcode. This time use the Post Selection tab in the popup to select the related Person post as the source of the title. This will display the related Person's name.
- Now insert the View of Persons Projects inside the Loop of the View of Projects, and insert the View of Projects somewhere to test it out on your site.

#1079442
CloudApp Annotation 2018-08-10 at 11.40.28.png.png

Thanks Christian, using your instruction as reference, I managed to get this far (see screenshot) — problem is, as you can see, the "project role" is showing up in every loop.

In this case, "Co-Principal Investigator" should only show up once, then followed by 3 names below.

You may view the actual page at hidden link

May I create a login account for you to go in take a look? Before I lose all my hair...!

#1079653

You may ignore my last question. I decided to use filter to query items by specifying custom field values. It's cumbersome, but gets the work done.

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