[Resolved] Unable to sort on a relationship custom field
This thread is resolved. Here is a description of the problem and solution.
Problem: I would like to sort a View of posts based on the value of a custom field applied to one of the post's relationships (intermediary post).
Solution: A View can only be sorted by fields applied to the post type being queried by the View. If you want to sort on a field in an intermediary post type, the View must query only that intermediary post type. If you want to display information from other posts in the View's results, you can use the Types field settings to choose a related post.
This support ticket is created 4 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.
Hi support team!
To start, here's a page where I've got a problem: hidden link
It is a view with buildings projects (= custom post type 1). In that view, there's another view that lists all the project owners (= custom post type 2) (= the little logo's on the building project picture).
Back in time, those logo's were in a random order. Now, our client want that they are ordened by importance.
So I've made a custom field (single line) inside the relationship 'building projects project owners' called 'Order'. I've set the order in the example project called 'west bay' and I changed my 'project owners' view to order by the custom field 'Order'.
When I do a page refresh: alle the related project owners are gone. When I set back the 'random order' sorting, alle the project owners are back again. It looks like I'm not able to sort on a relationship custom field.
You can only sort by fields of the thing being queried.
If you add relationship fields to a M2M relationship then these custom fields are stored against an intermediate post.
So the order custom field is stored against this intermediate post, and therefore only a View querying this intermediate post type can be ordered by that custom field.
You may need to update your View so that it queries the intermediate post type (change the Content Selection for the View). Then, in the output section it will be iterating over the matching intermediate posts, meaning you will likely have to change the relationship Query Filter that I expect your View to have, and for the output specify the relevant related post for the source of the fields being output (rather than the current intermediate post being the source).
Thanks for your feedback Nigel. What I did was creating a new view and trying to select the intermediate post type in the 'Content selection'-section. But I'm afraid I have not the option to select an intermediate post type. See screenshot 'content-post-types.png'.
Hi Christian, thanks for the reminder!
Hi Nigel, I did have a post type called "Bedrijven Projecten Intermediary Posts".
As asked, I did check the checkbox 'publicly_queryable' as a test. After that, I tried to query on it's intermediary custom field 'Volgorde'. But again: there were no results anymore. See screenshots.
Because the website is running live, I changed it to 'random' order again.
Perhaps there's something I missed because you said "etc" in your latest response?
After that, I tried to query on it's intermediary custom field 'Volgorde'. But again: there were no results anymore. See screenshots.
A couple more things to try:
- The View's old results could be cached by Views caching. You can disable Views caching by adding cached="off" to the View shortcode:
- Sometimes when similar field names are reused it is easy to select the wrong field in sorting or filtering. For example, if another field is named Volgorde but has a different slug, it is possible you have selected the wrong Volgorde field in sorting or filtering. That would cause unexpected results. I'll be glad to log in and take a closer look if you provide login credentials in the private reply fields here. I'd like to see how the View is set up and determine if there are any other factors we are overlooking.
This is the view with the building projects owners: "Frontend - Bedrijven (per project) (overzicht)" => Here should be the sorting on custom field "Volgorde" (nog on "random order" again)
This View's Content Selection is "Bedrijven", but if you want to filter or sort by a field on the intermediary post type you must select "Bedrijven Projecten Intermediary Posts", since the Volgorde field is on the intermediary post type. Then in the loop of your View, you can display information from the Projecten post type by using the Post Selection tab in the Fields and Views popup menu.
You can only sort or filter by fields that are attached to the intermediary post type when you select the intermediary post type in the View's Content Selection area.
The screenshot you took is not the right view that you were watching.
Instead, it's the view called 'Frontend- Bedrijven (per project) (overzicht)'. So I guess you've got the wrong information to provide your answer? 😉
Okay, but the Content Selection is wrong in this View as well. See the screenshot here. The Content Selection must be the intermediary post type if you want to sort or filter upon the Volgorde field.
Ah indeed, you're right! Now I understand the question from Nigel to check the option 'publicly_queryable'. Now I have the ability to check it in the content selection.
So here's what I did now:
- I duplicated the view 'Frontend - Bedrijven (per project) (overzicht)' .
- I called it 'Frontend - Bedrijven Projecten Intermediary Posts (per project) (overzicht)'.
- I changed the content selection to 'Bedrijven Projecten Intermediary Posts'.
- I changed the view shortcode in 'Frontend - Projecten (overzicht)' so the child view is pointing to the duplicated view.
It seems the problem still isn't solved when I change the ordering to 'Field - Volgorde'. But I guess we're very close now? 🙂
Hi Christian, forgot what I said in my previous response: it works now! The thing was that I only set values on one 'Building project'. All the other logo's are disappearing when there's no 'volgorde' (order) given as a custom field value. But I can live with that 😉
Thank you very much guys! Wasn't easy at all, but I do understand how to fix it in future project. Thumbs up for your great support again!