Support » Plugins and Hacks » Hacks » Grouping posts by tag in category archives

Grouping posts by tag in category archives

  • Tommy Pradana


    I need to list posts on category archives, grouped by the tag they are in, with the tag name above the group of posts. So the output would look something like this:

    <h1>'Brown' Category</h1>
    	<h2>'Dogs' Tag</h2>
    		Post 1
                    Post 2
                    Post 3
    	<h2>'Cats' Tag</h2>
    		Post 1
                    Post 2
                    Post 3

    it possible in archive pages? Or make a custom page….. idont uderstand…i need help

Viewing 1 replies (of 1 total)
  • bcworkz


    Yes it’s possible. The problem is deciding the best way to accomplish this. It’s complicated by all the different tag scenarios, multiple tags for one post, posts with no tags, etc.

    The most efficient way would be to filter the category query and alter it to order the results by tags. Since tags are part of the taxonomies, this is not a trivial task.

    Much easier would be to rearrange the results on the applicable template page. You could buffer the loop output, then rearrange the posts in the buffer before they are output.

    Or you could re-do the category query from the template page, this time sorting by tags. But again, you will need to join taxonomies into your ordered query.

    Even easier, and least efficient, is to ignore the original query results. Get a list of tags. Step through the list and query for posts of whatever category with the applicable tag.

    There’s probably even more possibilities. The best choice will depend on how much time you are willing to commit, your coding skills, and how big your database is. With small databases, efficiency is not too important. With large ones, it’s worth investing the time to develop efficient code.

Viewing 1 replies (of 1 total)
  • The topic ‘Grouping posts by tag in category archives’ is closed to new replies.