• Is it possible to link to a page with posts wich occur in both category 1 & 2?

    As the URI http://example.com/?cat=1,2 displays posts in both categories 1 and 2, I only want to display posts which occur in both categories.

    Thanks,

    Misund

Viewing 8 replies - 1 through 8 (of 8 total)
  • No I don’t think that’s possible without writing a custom query 🙁

    You could put both the categories into a parent category, and call that though.

    Thread Starter Just Thomas Misund

    (@dnusim)

    Looks like I’ll have to write a custom query then… 🙂

    But I don’t understand the parent category strategy.

    Let’s say I have posts about cafés (cat=1) and movie theaters (cat=2) in Scotland (cat=3) and Wales (cat=4). I put a post about a movie theater in Scotland in categories 2 and 3.

    I then want to display posts about cafés in Wales (posts in both cats 1&4) without displaying posts about cafés in Scotland or movie theaters in Wales.

    How do I do this with parent categories?

    Edit: As you might figure, my posts span over a lot more categories, and this is a simplification to easier get a handle of the case.

    bocajuniors

    (@bocajuniors)

    I am also looking for a way to do this.

    Frustrating!

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Let’s say I have posts about cafés (cat=1) and movie theaters (cat=2) in Scotland (cat=3) and Wales (cat=4). I put a post about a movie theater in Scotland in categories 2 and 3.

    I then want to display posts about cafés in Wales (posts in both cats 1&4) without displaying posts about cafés in Scotland or movie theaters in Wales.

    Use ...?cat=1,4,-2,-3. The minus in front of a category excludes those categories from the search. So you won’t get any posts about theaters or Scotland. You’ll still get posts that are only about cafes or Wales though.

    As for the more general question of treating the cat query as an AND instead of an OR… can’t do it. Not with the stock code. You’d have to add custom queries.

    bocajuniors

    (@bocajuniors)

    Any idea where in the WP code it does an “OR” rather than an “AND” when it is processing those ?cat=X,Y,Z variables?

    Thanks so much! 🙂

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    It actually doesn’t do either an OR or an AND. It does an “IN”. In SQL, you can do something like this:
    WHERE category IN (1,2,3,4)..

    That’s the equivalent of an OR, pretty much. There is no similar equivalent of an AND, except to make a really big WHERE statement and slow everything down.

    And it’s in query.php.

    bocajuniors

    (@bocajuniors)

    Thanks so much for the help.

    If you (or anyone!) has an idea of what such a WHERE statement would look like it would be greatly appreciated.

    I see the category section of query.php, but I’m not sure where/what to drop in.

    Thanks again.

    Did you guys figure something out to solve that problem? same problem here!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Display posts in two categories’ is closed to new replies.