Query examples
Query examplesBlocks

Blocks

These are examples of queries to fetch block data.

Fetching blocks in a custom post via a Block type

Fetching data for all blocks in a post:

{
  post(by: { id: 19 }) {
    blocks {
      ...BlockData
    }
  }
}
 
fragment BlockData on Block {
  name
  attributes
  # innerHTML
  contentSource
  innerBlocks {
    name
    attributes
    # innerHTML
    contentSource
    innerBlocks {
      name
      attributes
      # innerHTML
      contentSource
      innerBlocks {
        name
        attributes
        # innerHTML
        contentSource
        innerBlocks {
          name
          attributes
          # innerHTML
          contentSource
          innerBlocks {
            name
            attributes
            # innerHTML
            contentSource
            innerBlocks {
              name
              attributes
              # innerHTML
              contentSource
              innerBlocks {
                name
                attributes
                # innerHTML
                contentSource
              }
            }
          }
        }
      }
    }
  }
}

Retrieving only blocks of certain types:

{
  post(by: { id: 19 }) {
    blocks(
      filterBy: {
        include: [
          "core/heading",
          "core/paragraph"
        ]
      }
    ) {
      ...BlockData
    }
  }
}
 
fragment BlockData on Block {
  name
  attributes
  # innerHTML
  contentSource
  innerBlocks {
    name
    attributes
    # innerHTML
    contentSource
    innerBlocks {
      name
      attributes
      # innerHTML
      contentSource
      innerBlocks {
        name
        attributes
        # innerHTML
        contentSource
        innerBlocks {
          name
          attributes
          # innerHTML
          contentSource
          innerBlocks {
            name
            attributes
            # innerHTML
            contentSource
            innerBlocks {
              name
              attributes
              # innerHTML
              contentSource
              innerBlocks {
                name
                attributes
                # innerHTML
                contentSource
              }
            }
          }
        }
      }
    }
  }
}

Excluding blocks:

{
  post(by: { id: 19 }) {
    blocks(
      filterBy: {
        exclude: [
          "core/heading",
          "core/paragraph"
        ]
      }
    ) {
      ...BlockData
    }
  }
}
 
fragment BlockData on Block {
  name
  attributes
  # innerHTML
  contentSource
  innerBlocks {
    name
    attributes
    # innerHTML
    contentSource
    innerBlocks {
      name
      attributes
      # innerHTML
      contentSource
      innerBlocks {
        name
        attributes
        # innerHTML
        contentSource
        innerBlocks {
          name
          attributes
          # innerHTML
          contentSource
          innerBlocks {
            name
            attributes
            # innerHTML
            contentSource
            innerBlocks {
              name
              attributes
              # innerHTML
              contentSource
              innerBlocks {
                name
                attributes
                # innerHTML
                contentSource
              }
            }
          }
        }
      }
    }
  }
}

Fetching block data in a custom post via a JSONObject type

Fetching data for all blocks in a post:

{
  posts(by: { id: 19 }) {
    blockDataItems
  }
}

Retrieving only blocks of certain types:

{
  posts(by: { id: 19 }) {
    blockDataItems(
      filterBy: {
        include: [
          "core/heading",
          "core/paragraph"
        ]
      }
    )
  }
}

Excluding blocks:

{
  posts(by: { id: 19 }) {
    blockDataItems(
      filterBy: {
        exclude: [
          "core/heading",
          "core/paragraph"
        ]
      }
    )
  }
}

Fetching flattened block data in a custom post

Field blockFlattenedDataItems flattens the block hierarchy contained in the custom post to a single level. Then, filtering by block type will also include inner blocks who have a parent block that is excluded.

Fetching data for all blocks in a post:

{
  posts(by: { id: 19 }) {
    blockFlattenedDataItems
  }
}

Retrieving only blocks of certain types:

{
  posts(by: { id: 19 }) {
    blockFlattenedDataItems(
      filterBy: {
        include: [
          "core/heading",
          "core/paragraph",
          "core/columns",
          "core/column"
        ]
      }
    )
  }
}