wp-graphql / wp-graphql-tax-query

Adds `tax_query` support to postObject connection queries using WP_Query
46 stars 16 forks source link

Not working for pages #10

Closed drewbaker closed 5 years ago

drewbaker commented 5 years ago

I have a custom taxonomy called type. It is used on pages.

The below taxQuery doesn't work, it returns empty results. There are 11 pages in the type category with slug latest.

query{
  pages(
    where: {
      taxQuery: {
        taxArray: [
          {
            terms: ["latest"],
            taxonomy: TYPE,
            operator: IN,
            field: SLUG
          }
        ]
      }
    }
  ){
    edges{
      cursor
      node{
        slug
        title
      }
    }
  }
}

And the results are:

{
  "data": {
    "pages": {
      "edges": []
    }
  }
}
jasonbahl commented 5 years ago

@drewbaker I just tried and this seems to work for me:

{
  pages(
    where: {
        taxQuery: {
            taxArray: [
                {
                   taxonomy: CUSTOMTERM, 
                   terms: ["New Term"], 
                   field: NAME
                }
             ]
          }
       }
    ) {
    nodes {
      id
      link
      slug
    }
  }
}

Here's the steps I took:

Register Custom Taxonomy connected to Pages:

add_action( 'init', function() {

register_taxonomy(
    'custom_tax',
    'page',
    array(
        'public'            => true,
        'hierarchical'      => false,
        'labels'            => array(
            'name'          => _x( 'Custom', 'post format' ),
            'singular_name' => _x( 'Custom', 'post format' ),
        ),
        'query_var'         => true,
        'show_ui'           => true,
        '_builtin'          => true,
        'show_in_rest'      => true,
        'show_in_graphql'   => true,
        'graphql_single_name' => 'CustomTerm',
        'graphql_plural_name' => 'CustomTerms'
    )
);

} );

Assign a page to "New Term" in that taxonomy

Screen Shot 2019-05-06 at 7 49 50 AM

Test the query:

Screen Shot 2019-05-06 at 7 50 58 AM

drewbaker commented 5 years ago

Weird, this is totally not working for me.

Do you see anything I am missing?

Screen Shot 2019-05-06 at 1 30 17 PM Screen Shot 2019-05-06 at 1 30 11 PM Screen Shot 2019-05-06 at 1 30 35 PM

drewbaker commented 5 years ago

For future people: So I spoke with Jason today and found a little hidden setting, by default all page queries have parent: 0 set on them, so if you want any depth page, then you need to set parent: null in the where clause! This ended up working!

{
  pages(
    where: {
        parent: null,
        taxQuery: {
            taxArray: [
                {
                   taxonomy: TYPE, 
                   terms: ["colour"], 
                   field: NAME
                }
             ]
          }
       }
    ) {
    nodes {
      id
      link
      slug
    }
  }
}