Skip Navigation

[Resolved] Sorting View Results by Taxonomy Term Custom Field

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

Problem:

I'm trying to sort a list posts by custom field associated with a taxonomy term. The purpose of this is to assign priority to the taxonomy term via a numerical value, so that the posts associated with the taxonomy term will be listed in accordance to that priority.

Solution:

It just needs a nested view, for example:

https://toolset.com/forums/topic/sorting-view-results-by-taxonomy-term-custom-field/#post-1154414

Relevant Documentation:

https://toolset.com/documentation/user-guides/using-a-child-view-in-a-taxonomy-view-layout/

This support ticket is created 6 years 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by Luo Yang 5 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#1150756

I'm trying to sort a list posts by custom field associated with a taxonomy term. The purpose of this is to assign priority to the taxonomy term via a numerical value, so that the posts associated with the taxonomy term will be listed in accordance to that priority.

For context, I have a list of employees, each associated with a "Position" taxonomy term. There are levels of priority associated with each taxonomy term (ie. Principal Agent [priority level 1] > Receptionist [priority level 7]), so in the name of ease of access for the client, when they add or remove employees, any new or existing employees will be sorted based on the priority level of their position.

I'm not following any particular documentation, but I was trying to follow this support thread, but got lost in the explanation: https://toolset.com/forums/topic/sort-view-by-taxonomy

Here's a link to the page in question on my site: hidden link

From my understanding, Toolset doesn't have any out-of-the-box solutions for this. Is there a way we can adjust the solution found in the aforementioned support thread to support a custom field and only one sorting mechanism?

#1151097

Hello,

You are right, Views is using wordpress class WP_query to query posts:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
There isn't such an option to order the result by taxonomy

So as a workaround, you can use custom codes to setup a field to store the taxonomy value, then use this custom field as sortby parameter.
In the thread you mentioned above, that user is using a CRED form to create new post, so it is using action Toolset form hook "cred_save_data" to setup the custom field, see the codes here:
https://toolset.com/forums/topic/sort-view-by-taxonomy/page/2/#post-517595

If you need assistance to setup the similar custom PHP codes, please provide a test site with the same problem, also point out the problem page URL and form URL, and where I can edit your custom PHP codes, I need a live website to test and debug.

#1153526

Thanks for the details, I can login your website, but I need more details:
There is only one Toolset post form in your website: Alert Submission
Are you going to use this form to update the "Alert" post custom field?
If it is, please provide the front-end URL, where I can test it?
Where one is the taxonomy field? which one is the custom field you want to update?

Thanks

#1153985

That's not even related.

The client isn't going to be submitting a CRED form to add employees. They'll just be logging into the WordPress admin to do this.

What I need to do, is take the data from the "Employee" custom post type and sort the results in a view by their association to a "Position" taxonomy term. Each taxonomy term under "Position" has a custom term field that contains a numerical value that denotes its priority (ie. Principal Agent [priority level 1] has a greater priority over Reception [priority level 7]). The sorting does not need to be changed by the end user. The whole purpose of this is to parse the data out into a logical manner, grouping individuals by their job positions, and then sorting them by their position's priority.

Here are the views and layouts in question:

Layout for Locations: hidden link

Employee List View: hidden link

Positions Taxonomy Terms: hidden link

#1154414

Thanks for the details, in your case, it does not need custom codes, it just needs a nested view, for example, I have done below modifications in your website:

1) Create a taxonomy view "Parent taxonomy view - Positions"
hidden link
query terms of taxonomy "Position", order by term field "priority", in the view's loop, display the child post view "Employees List View":

[wpv-taxonomy-title]: 
 [wpv-view name="employees-list-view"]

2) Edit the post post view "Employees List View", add a filter:
Select posts with taxonomy:
Positions set by the parent Taxonomy View

3) Edit the layout "Layout for Locations":
in section "Our Team", display the taxonomy view's shortcode "Parent taxonomy view - Positions" instead of post view:
[wpv-view name="parent-taxonomy-view-positions"]

Test it in front-end:
hidden link

Please check if it is what you want. thanks

More help:
https://toolset.com/documentation/user-guides/using-a-child-view-in-a-taxonomy-view-layout/
Views Inside Views – Display Nested Multidimensional Lists

#1158093

It seems to be operating in a manner that works for what I need. Thank you so much for your help!

#1158217

You are welcome