I have a requirement for displaying a list of custom post types grouped by category, but in addition to this, each custom post is linked with a meta value (in this example lets say we have a custom meta value of "TEST1").
Basically, my goal is to display a list, grouped by category for all custom posts with a code of "TEST1". The way I am currently doing this is by simply getting all categories linked to my custom post type (ignoring empty), then looping through each category to get all posts within it that have a code of "TEST1".
Now this is great, it works fine, however it is extremely inefficient. At the moment this isn't an issue but I fear that it won't scale very well as the number of categories increases overtime - because each iteration equals another DB hit (and join to the post meta table).
I have had a good look across the web but I am unable to find anything but was wondering if it is possible to perform a WP_Query call that would return all posts with the custom meta value of "TEST1" grouped or ordered by their category name? This would be a lot more efficient (basically 1 DB hit I would assume). If anyone can advise anything here I would be really grateful, if it is not possible then I assume my only way to achieve this would be to do a direct query to the database from within my plugin (which from what I've read is not advised when working with plugins).