How to show posts that are in BOTH of two categories on a WordPress search results page

21 September 2019

Previously I've written about how to show posts from more than one WordPress category on one search page (ie posts from category A and posts from category B). But what if you want to show only posts that are in BOTH category A and category B? That's possible too, though it's perhaps done a slightly counter-intuitive way.

Let's take two of the categories on this site, 'web development' and 'online advertising':

https://www.technicallyproduct.co.uk/web-development/
https://www.technicallyproduct.co.uk/online-advertising/

It is also possible to use the category IDs as URL parameters to get the same results (actually these latter URLs just redirect to the former ones):

https://www.technicallyproduct.co.uk?cat=13
https://www.technicallyproduct.co.uk?cat=23

Brilliantly, you can combine these two approaches to see only posts that are in both the 'web development' and 'online advertising' categories.

https://www.technicallyproduct.co.uk/web-development/?cat=23
https://www.technicallyproduct.co.uk/online-advertising/?cat=13

The two pages list the same posts but are not completely the same themselves.

The first is the 'web development' category page filtered to show only posts that are also in 'online advertising', so it has the 'web development' page furniture (for example the intro text at the top of the page).

Conversely, the second is the 'online advertising' page filtered to only show posts that are also in 'web development', so has the 'online advertising' page furniture.

Looking online, other people have posted different approaches to the same problem (for example using a URL parameter with the format ?cat=firstcat+secondcat). None of these seem to work for me - that is not to say they would not work for you or your site.

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © James Clark
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram