• Hi. I’m trying to find a way to display posts by a taxonomy over another taxonomy (if that makes sense). I found several topics online, but i don’t know how to apply those solutions to my situation.

    I have a post type called “services” and 2 taxonomies, one called “region” and the other “service_category”.
    The region tax have 15 terms (region 1, region 2…), and in the service_category tax have 13 terms (sports, health, fashion…).
    The “service_category” terms must be applicable to each “region”term.

    The idea is that a user choose one of those regions, let’s say “region1” and, in the next page, selects one “service_category” like “sports”. I need to display all the posts that fit, first in region 1, and then sports.

    And maybe another user selects region 2, then I need to filter that region by the same “service_category” tax.

    I should mention that the users can post services in a frontend form, so I can’t implement a solution like create child terms like “sports” on every “region” term. Because when the user selects in what region post its service, he would see something like
    region1
    — sports
    — fashion
    region2
    –sports
    –fashion

    So that solution doesn’t apply to me. What I need is that the user select the “region” in one field and the “service_category” in another field. That’s done, I have no problem with it, but I having trouble to think a logical and correct way to display posts based on a “region” and “service_category”.

    And the other thing. What should be the correct template hierarchy for this situation?

    Thank you all.

Viewing 4 replies - 1 through 4 (of 4 total)
  • My favorite way to do this was to use Query Multiple Taxonomies, but it is no longer supported. It does have some recommendations of other plugins to try instead on the page, though.
    There is also a plugin that I got years ago called Taxonomy Picker, not in the WP repository, but I can’t see where to download it now.

    This type of query is also called filtering or faceted search, and there are quite a few plugins for that, although a bunch are specific to the various shopping cart plugins’ post types.

    Moderator bcworkz

    (@bcworkz)

    If you’re comfortable with coding your own solution, it’s not all that involved if you already have the taxonomies and form elements working. There’s no template hierarchy needed, it can all be handled from one taxonomy archive template that your theme already uses. You’d be making a normal service_category request as though it were the only term, but alter the query made to also incorporate the selected region. The request from the form might look something like: example.info/service_category/sports/?region=region1

    Alter the query through the “pre_get_posts” action. Your callback would take the “service_category” and “region” query vars to compose a “tax_query” query var that combines the two terms into one query var. Leaving the two set separately will confuse WP. Unset the original “service_category” and “region” vars since they’re now dealt with in “tax_query”. WP will then handle the rest.

    If that makes no sense to you at all, it’s OK, perhaps it’ll help someone ?? If that’s the case, consider the plugin suggestions Joy made.

    Actually, you don’t need any special code to have WP query multiple taxonomies; no pre_get_posts is needed. Both ugly and a mix of ugly and pretty permalinks work just fine.
    You just have to structure the links so that the query variables are set correctly, meaning that if it is two terms of the same taxonomy, they are listed like ?tax1=one+two whereas if it is two separate taxonomies, it is ?tax1=one&tax2=two.
    The trick though, is for the template to label it correctly. Most themes will just call the_archive_title(), which is only going to give you one result, so you need to filter that to check for multiple.

    Thread Starter gfpunto

    (@gfpunto)

    Thank you both! your comments gave some path to learn.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Filter posts by 2 taxonomies’ is closed to new replies.