If I understand what you’re after, I think you just need some custom code within the loop to alter the output if the current post has a taxonomy term assigned. I’m assuming the query returns all the posts properly and they are ordered the way you want, it’s just the way they are output is what isn’t working.
You can use wp_get_post_terms() to get the current post’s terms if they exist. If this function returns an empty array, there are no terms and the normal output will work. If there are terms in the returned array, you could use that term data to output something different. For example, instead of the usual title, when a term is assigned to a post we output something like “Some images of Foo” where the assigned term is “Foo”. And maybe instead of the excerpt, we output only the featured image. Just about anything is possible if the data is available.
This is all done by customizing whatever template is used for the output. When you customize templates, you should create a child theme so your custom code is not lost when the theme updates.
This is all done in my own theme so there shouldn’t be any issues with child themes.
I’ll investigate the wp_get_post_terms(), but my immediate reaction is that will create lots of the same titles for posts.
E.g. if i have the following products:
Adidas
Nike
Reebok
Puma
Umbro
Nike and puma are in the taxonomy ‘premium’
So the loop as I want it would output:
Adidas
Reebok
Premium
- Nike
- Umbro
Puma
I can’t help but feel it will output:
Adidas
Reebok
Premium
- Nike
Premium
- Umbro
Puma
As i’m not looping through the term to get the posts? if that makes sense?
Either way i’ll investigate further!
Also, once i’m in the post loop, it’s going to throw the A-Z order out, as I’ll be then telling it to show the taxonomy name of the post, which would be different from the post name.
Yeah, I didn’t quite follow what you’re after, you are correct. You could add additional code that detects when the category changes from the previous post so the “Premium” header is only output once per group. If it changes from normal to premium, output the Premium header and maybe indent subsequent posts. When the category changes from premium to normal, do not indent subsequent posts and do not output any header.
If the one category is something you wish to highlight, you could run two loops from the same query results. Before either loop starts, output the “Premium” header. The first loop will only output the premium posts, ignoring the other posts. Then call rewind_posts() to reset the loop counters and run the second loop, which ignores premium category posts and outputs everything else.
Ok so it’s almost as if i need code that goes through the loop and if it finds a taxonomy it checks if there are any other posts in that taxonomy, and maybe adds that to an array to say it’s been ‘checked’ so that when it gets to a product further down the line in the same taxonomy as before it knows it’s already been dealt with? i.e (in very rough code)
$array = []
if (post_has_term and is not in $array ) {
output the_term_name
foreach (post in post_has_term) {
output the_title()
}
the_term_name push in to array
else {
output the_title()
}
i could possibly make that work (although there must be a simpler way?) but the one thing that still doesn’t help me do is alphabetise everything. If the product was aardvark in the category zoo animals then the list would go
Zoo Animals
Giraffe
Lion
because it’s finding aardvark first but outputting it’s taxonomy name?
If you follow that logic?