I want to query a custom post type that has 100+ associated categories. The query will be based on values in the current user meta, specifically, the user will have preselected categories they want to see.

What is the best way to do the query that: returns all posts that match at least one category while ordering by post with the most number of categories matching the user preference?

1 Answer 1


I don't think this can be cleanly expressed in WP query API. YOu can query for posts matching all of terms in a set or any of terms in a set, but there is no concept of how many terms matched.

You would need to write either:

  1. Very custom SQL for the query you want which will implement the counting.
  2. Query loosely everything that matches and do the coun on PHP side after getting results.

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.