Skip to content

GraphQL Queries

Learn how to query data from your WordPress backend using GraphQL.

Basic Queries

Fetch Posts

const GET_POSTS = gql`
  query GetPosts($first: Int, $after: String) {
    posts(first: $first, after: $after) {
      pageInfo {
        hasNextPage
        endCursor
      }
      nodes {
        id
        title
        excerpt
        content
        date
        author {
          name
        }
      }
    }
  }
`;

Fetch Single Post

const GET_POST = gql`
  query GetPost($id: ID!) {
    post(id: $id) {
      id
      title
      content
      date
      featuredImage {
        sourceUrl
      }
    }
  }
`;

Fetch Pages

const GET_PAGES = gql`
  query GetPages {
    pages {
      nodes {
        id
        title
        content
        uri
      }
    }
  }
`;

Advanced Queries

Filtered Queries

const GET_POSTS_BY_CATEGORY = gql`
  query GetPostsByCategory($categoryId: ID!) {
    posts(where: { categoryId: $categoryId }) {
      nodes {
        id
        title
      }
    }
  }
`;

Search Queries

const SEARCH_POSTS = gql`
  query SearchPosts($search: String!) {
    posts(where: { search: $search }) {
      nodes {
        id
        title
        excerpt
      }
    }
  }
`;

Pagination

Use cursor-based pagination for efficient data loading:

const GET_POSTS_PAGINATED = gql`
  query GetPostsPaginated($first: Int, $after: String) {
    posts(first: $first, after: $after) {
      pageInfo {
        hasNextPage
        hasPreviousPage
        startCursor
        endCursor
      }
      nodes {
        id
        title
      }
    }
  }
`;

Next Steps