Skip Navigation

[Resolved] How to order a view by a field in a Parent Post Type

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

Problem:
How is it possible to order a View of one post type by a field in a parent post type?

Solution:
It's not possible, whatever is being displayed in the View can only be ordered by properties of the thing itself.

To order by properties of something else would require a custom DB query with table joins that is not currently supported by Views.

Future versions using the new post relationships will see this functionality added.

100% of people find this useful.

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

Last updated by Furlan 6 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#607839
2018-01-18_20-48-35.png

I have a view from a Many-to-Many Relationship.
TASKS<---ASSIGNMENTS---->RESPONSIBLE

The View is based on ASSIGNMENTS in order to display fields from both TASK and RESPONSIBLE tables. For instance "Task No" from "TASKS" table and "Responsible Name" from "RESPONSIBLE". The problem is that the results in the view are shown unordered and I need them ordered by "Task No" in correlative order, 1, 2, 3, 4...

How could I make my view to display the results order by "Task No" when this field is not in the table "ASSIGNMENTS".

If I choose "Task No" as "Order by" in "Ordering Section" from Views and I obtained "No results" message.

#607915

Nigel
Supporter

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

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

Hi Juan

It is currently not possible.

As you rightly identify yourself in the question, you can only order something (assignments) by properties of the thing itself.

Task No is not a property of assignments, and so cannot be used to order assignments.

You have 3 options:

1. duplicate the Task No field on the assignments post type so that it can be used for the orderby condition. You could use the WordPress save_post hook (https://codex.wordpress.org/Plugin_API/Action_Reference/save_post) to do this automatically whenever an assignment post was saved, get the parent Task No and save that to the assignment post meta.
2. Write a custom SQL query with a table join to connect the Task and Assignment post types and effectively make the Task No property available to the assignment post type. It's beyond the scope of the support I can give to provide such a solution.
3. Wait. The updates to post relationships currently being worked on will add the ability to filter by parent post fields and use them to order results. I don't believe that will be included in the initial release, but it is on the roadmap so should be added in a subsequent update.

Let me know if you try any of these and run into problems.

#608043

Thanks Nigel for your support.

I went with a temporary solution, add" Task No" field in ASSIGNMENTS and feed it manually by now.
I'll wait for the version release where this action hopefully will be available.