WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display posts in two categories (9 posts)

  1. Just Thomas Misund
    Member
    Posted 8 years ago #

    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

  2. maerk
    Member
    Posted 8 years ago #

    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.

  3. Just Thomas Misund
    Member
    Posted 8 years ago #

    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.

  4. bocajuniors
    Member
    Posted 7 years ago #

    I am also looking for a way to do this.

    Frustrating!

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  6. bocajuniors
    Member
    Posted 7 years ago #

    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! :)

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  8. bocajuniors
    Member
    Posted 7 years ago #

    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.

  9. maxblast
    Member
    Posted 7 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic