I have set up a View where the Content Selection is various different Custom Post Types. I would then like to group the results of the Query into different groups by CPT.
For example, If I have CPTs of Fruits, Cities, Countries. I would like all the results that are CPT Fruits to be displayed together, then all the Cities to be displayed in some sort of group together and then on and on in all the different CPTs.
Is this possible in any way?
Hi, Post type Ordering is available as a primary sort order option in the View editor. If you cannot see the Ordering options in the View editor screen, scroll to the top right corner of the page and click "Screen Options". You can enable the Ordering section here, then select a primary order by post type and a secondary order by some other criteria like post date. The options for post type order are limited to alphabetical sorting, so if you want a different sort order, a custom code approach is required. We offer the wpv_filter_query API to help modify query parameters: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Thank you for your suggestion Christian.
That solution works, but it's not what I was looking for. Sorry, I should have explained what I require in more detail.
I want to do a similar thing to what you have suggested. Instead of the results being ordered by different Custom Post Types one after the other, I would like the results to be displayed in different lists by Custom Post Type. For example, a list of all results of CPT type A, then a separate list for all results of CPT type B.
I hope this makes more sense now?
Okay I see what you mean. There's not an easy way to split up a single View loop by post type like this. You could create multiple Views, one for each post type, and display them one after another. If custom search is used, each View's Query Filters should be set up to respond to the same URL parameters so one set of controls can be used to filter all the Views. It will be tricky if you have lots of results and plan to use pagination.
Is the multiple View approach possible in your case?
Possibly, but there will be large amount of results, and results will be added all the time.
What sort of problems would I be likely to encounter doing it this way?
I try my best to stay away from displaying URL parameters wherever I can, as I find them to look messy, so a solution without them would be ideal (if one exists!).
What sort of problems would I be likely to encounter doing it this way?
It's hard for me to say without knowing exactly how you want to set this up. Can you provide a similar example, or a mockup sketch so I can see what you would like to accomplish? That will help me understand how you want to paginate and separate results. A separate mockup showing a second page of results will also be helpful.
Please find attached a mock up of the sort of thing I want to do.
As long as I can achieve these basics, then I'm not too worried about anything else.
It's quite basic, so let me know if there is anything else or any further detail you require.
At a very basic level, you can accomplish something like this with 3 Views. However, it requires the use of URL parameters to define search criteria for each filter. The 3 Views' Query Filters must be set up to respond to URL parameters, pagination must not be used, and AJAX updates are not supported. If those criteria are acceptable, I can help you set something like this up in a test environment so you can see how it works, then determine if it is in line with what you want to accomplish.
Thank you for the suggestion Christian.
I've managed to set this up myself and it seems to do what I require. I just wish the URL parameters didn't have to be visible, but you can't have everything I suppose!!
I'll see if there is another way I can achieve this outside of Toolset if there are no other options?
I think I've covered all the available options in Views. We have discussed:
- Custom code with the wpv_filter_query API
- 3 separate Views responding to URL parameters
- 1 View with all results ordered by post type
Let me know if you have questions about any of those available options.
Thank you for all your help with this Christian, it's really appreciated.
I think I will have a go at achieving this another way.