I would like to sort posts by tags in category and archive pages. (Unfortunately there isn’t an orderby
parameter for tags. That would make things so easy!)
So a category page would be like this:
Category name
- Post 1 (Tag 1)
- Post 2 (Tag 1)
- Post 3 (Tag 1)
- Post 4 (Tag 2)
- Post 5 (Tag 2)
- Post 6 (Tag 2)
- Post 7 (Tag 3)
- Post 8 (Tag 3)
- Post 9 (Tag 3) ...
Or, even better:
Category name
Tag 1
- Post 1
- Post 2
- Post 3
Tag 2
- Post 4
- Post 5
- Post 6
Tag 3
- Post 7
- Post 8
- Post 9 ...
The tags would appear in alphabetical order. The posts below the tags would also appear in alphabetical order.
When a post has more than one tag, it needs to appear multiple times (one for each tag).
Is there any way to do that?
I thought about doing something like this, but I couldn’t come up with a solution that would show only the posts and tags in the category. (The linked code shows all posts and tags.)
Or maybe there is a way to treat tags as a meta_value
? Then I could simply use pre_get_posts
like this:
add_action( 'pre_get_posts', 'archive_post_order');
function archive_post_order($query){
if(is_archive()):
$query->set( 'orderby', 'meta_value' );
$query->set( 'metakey', 'tag' );
$query->set( 'order', 'ASC' );
endif;
}
Any ideas are welcome. Thanks in advance.
tags
?tax_query
to filter back to posts in the category..? just a thought...