How Can I Query WordPress Posts and Sort by Their Relevance?

Ashley Wright

September 29, 2017

WP_Query allows us to query our WordPress posts and return results based on the taxonomy terms those posts are tagged with. It does this quite well, and can then sort the posts by date, alphabetically, menu order, etc. However, it isn’t able to sort the results based on how relevant the post is – meaning how well it actually matches the selected terms.

After quite a bit of Googling, I wasn’t able to find the perfect solution for my needs, but I did find two solutions I could merge together.

Disclaimer: the edited plugin hasn’t been fully tested for all possible scenarios. However it did work for mine, which was querying a custom post type using multiple taxonomies using the ‘OR’ relation.