Get all the related posts except the current post using WPGraphQl in reactjs

Get all the related posts except the current post using WPGraphQl in reactjs


2

I’m trying to grab the related posts except the current post using WPGraphQL. My thought is to exclude the ID of the current post and display all related blog posts within the category.

I’m new to GraphQL, I don’t know how to figure out to solve it. is this possible in GraphQL? Please help

query MyQuery {
  category(idType: DATABASE_ID, id: 1) {
    posts {
      edges {
        node {
          id
          title
        }
      }
    }
  }
}

1 Answer
1


0

Use the where.notIn argument on the posts connection field under category, like so.

query MyQuery($currentPostId: ID) {
  category(idType: DATABASE_ID, id: 1) {
    posts(where: {notIn: [$id]}) {
      edges {
        node {
          id
          title
        }
      }
    }
  }
}



Leave a Reply

Your email address will not be published. Required fields are marked *