WPGraphQL/GraphQL custom post type filter

Viewed 519

I have created custom post type with 2 taxonomies, which I want to filter first by country taxonomy and then by practice area taxonomy. The problem is that I'm not allowed (can't find how to do it in docs) to do filtering in one step. The goal is to achieve all post types "experience" from "experience_countries" - "estonia" taxonomy, and that have "practice_area_category" - "all" taxonomy.

So graphQL schema allows me to do like this:

My current query

What this query returns:

Query returns

Instead of my goal, I'm getting post types for all countries (estonia and latvia, instead of only estonia) that match "practice_area_category" - "all" taxonomy.

I would greatly appreciate, any advice!

2 Answers

From what I understand you want to filter posts by experienceCountries then further filter with experiencePracticeAreas.

You don't need two separate custom taxonomies, the first custom taxonomy is enough "experienceCountries".

In WordPress do the following:

In experienceCountries taxonomy/category you have "Lativa", "Estonia" ...etc In your custom post type go to the experienceCountries category and add a new name for example "all" or "town name" whatever and from the dropdown option set parent category "estonia"

So that you have "estonia" as a parent and "all" as a child

Your GraphQL Schema:

experienceCountries(where: {slug: "estonia"})  {
 nodes {
  children(where: {slug: "all"}) {
    nodes {
      experiences {
          nodes {
            title
          }
        }
      }
    }
  }
}

I have a custom post type call books and taxonomy call author. In order to filter books by author I use this plugin:

https://github.com/wp-graphql/wp-graphql-tax-query

And my query is like this one.

{
  books(
    where: {
      taxQuery: {
        taxArray: {
          taxonomy: AUTHORTAG, 
          operator: AND, 
          terms: "john-doe", 
          field: SLUG
        }
      }
    }
  ) {
    nodes {
      databaseId
      title
      authorTags {
        nodes {
          databaseId
          slug
        }
      }
    }
  }
}

I think it could work with your custom post type and both taxonomies. You need to filter in the taxQuery with multiple taxonomy values using AND operator

Related