Skip Navigation

[Resolved] Sort view with a taxonomy filter by category, subcategory and title

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

Problem: How to order / sort Posts or Products by Taxonomy Terms or categories in a Post View?

Solution: Toolset Views does not allow ordering by Taxonomy Terms or categories. To modify sorting for results by Taxonomies it would require custom coding and use of wpv_filter_query_post_process OR wpv_filter_query to modify the results: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For more info you can refer to this on how to utilize wp_query arguments: https://www.billerickson.net/code/wp_query-arguments/

This could be possible using custom coding to make it work, we have some recommended list of service providers here who can implement such things for you: https://toolset.com/consultant/

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.

This topic contains 2 replies, has 2 voices.

Last updated by Kirill 3 years, 2 months ago.

Assigned support staff: Noman.

Author
Posts
#549483

I have a view that lists all products (a custom post type) sorted alphabetically. This view has two front-end filters: by product category and subcategory (in one drop-down) and by producer (a custom taxonomy, in another drop-down). Filters are located at the top of the page above the product list.

What I need to do is to change a sort order so that items be sorted by product category first, then by product subcategory, and finally by title.

Unfortunately, sort by product category is not available for the product view. It seems that I can use only product fields and product custom fields for that.

I was thinking about two ways to achieve what I need:

1. Create some kind of a custom sort function that will be called by the view and provide a sort order I need. However, I'm not sure if Views support that.

2. I can create a desired sort order with nested views (i.e. taxonomy category view as a parent, taxonomy subcategory as a child, and a product view as a child of a subcategory view). This displays results in an order I need, but it seems to loose an ability to use front end filters. It looks like front-end filters for taxonomy views are not available as filters toolbar is hidden. Also, it would make no sense to filter product category by producer as there is no producer info in a product category. Producer info belongs to the product itself, not to a category that forms a top-level view. I can add product filter to a product view level, but then this filter would be repeated for each product category/subcategory. I, however, need to have one filter at the top for all product items instead. So this seems to be not an option either.

Is there any way to achieve what I need within Views? What's the best way to do so? Please let me know if you need any additional information from me.

#549553

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Kirill,

Thank you for contacting Toolset Support. Toolset Views does not allow ordering by Taxonomy or categories. To modify sorting for results by Taxonomies it would require custom coding and use of wpv_filter_query to modify the results:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For more info you can refer to this on how to utilize wp_query arguments:
hidden link

This could be possible using custom coding to make it work, we have some recommended list of service providers here who can implement such things for you: https://toolset.com/consultant/

Thank you

#549840

Thank you very much, Noman!

View filters are great. I think a can't use wpv_filter_query to provide a desired sort order because there is no option to group by a category within WP_Query. However, I think I can try to use a wpv_filter_query_post_process filter to sort items in a desired order after they are retrieved and before they are shown.

Plan B is to create a custom 'sort_order' field within a product that will be calculated each time a post is updated (or have a batch script that will update indexes). I can use this field then to sort products the way I need. It would probably be faster as there will be no taxonomy-sorting overhead any time a view is shown. It might work as some kind of 'crude' DB index.

Thanks for you help, I understand my options much better now.