0

Using headless WordPress and WPGraphQL, when a page is set to draft status, and I try to pull in pages into my React navigation, the site breaks. How can I either filter out pages/posts that are set to draft status, or pull them in and then filter on the frontend?

query appQuery($locationId: MenuLocationEnum!) {
  menuItems(where: { location: $locationId }) {
    edges {
      node {
      id
      title
      label
      menuItemId
      connectedObject {
        __typename
        ... on Post {
          id
          slug
          status <--Provides "publish" or "draft" in GraphIQL in WordPress dashboard
        }
        ... on Page {
          id
          slug
          pageId
          status <--Provides "publish" or "draft" in GraphIQL in WordPress dashboard
        }
      }
    }
  }
}
Matt
  • 1,561
  • 5
  • 26
  • 61
  • you can filter after fetching ... or add a privacy filter (`add_filter('graphql_data_is_private',...` - `return true` if object (`$model_name`) is a post/page and parent is a menu item) ... or add a custom 'where' option to make it conditionally queryable – xadm Jan 26 '21 at 21:42
  • @xadm - Could you expand on the `(add_filter('graphql_data_is_private',...)` filter option? I've tried to apply a `where` option in my GraphQL query, but it doesn't seem to work. I've tried `where: { status: [DRAFT, PUBLISHED] }` for `menuItems`, but this doesn't work. Am I applying it correctly? – Matt Jan 27 '21 at 16:31
  • it doesn't work because it is not supported - NONE of these options ... but it (WP GraphQL API) is expandable/customizable ... 2nd: you can extend API to support `status` option for `where` arg - there is no such option now, you can see in graphiql - it's required to write some plugin code ... as in 1st option, use this filter in your plugin ... or some other place/hook/filter where you can customize entire WP/SQL behaviour, e.g. 'pre_post' – xadm Jan 27 '21 at 16:46

0 Answers0