Let's say you want to show posts tagged with tags : "HTML, CSS".

I want to order posts in tag archive in the following order:

Post tagged with only exact two tags - HTML,CSS
Post tagged with only exact two tags - HTML, CSS
Posts tagged with three tags - HTML, CSS, jQuery
Posts tagged with three tags - HTML, CSS, ZEND
Posts tagged with four tags - HTML, CSS, JavaScript, PHP

So first result/s should only output posts with only exact specified tags. After there are no more posts tagged with only those two tags, output posts tagged those 2 tags + only one more tag. And so on.

What should I use? Two wpQueryPosts loops? Can it be done with only one loop? How to sort by matched tags?

  • 1
    Please show us what you tried. Else it gets quickly very opinionated. Just file an edit and add your code.
    – kaiser
    Nov 18, 2013 at 23:36

1 Answer 1


You can count the number of tags when a post is saved and store it as a meta key for the post. Now you can just add a sort by the value of the meta key to the tag page query.

  • We're on to something. I modified tag.php in twentythirteen. It sorts them as I wanted BUT seems like my query broke tag recognition. What I mean is it sorts them by number of tags, but completely ignoring what tags are selected. So it shows all posts with all tags and only sorts them by meta_value. How to fix this? I know there is tag argument for wpquery but how to include selected tags? $_GET[tags]? Will this even work with pretty permalinks? Anyways, I appreciate your help. Here's the pastebin of my tag.php pastebin.com/Rfcp7bT2
    – dev2pm
    Nov 21, 2013 at 10:39
  • you don't populate the parameters from the parameters of the original query. If you still can't figure it out please make it a separate question Nov 21, 2013 at 15:09
  • I used $tag = get_query_var('tag') and passed it to my custom wpQuery. Now it works like charm. Thank you @Mark
    – dev2pm
    Nov 22, 2013 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.