Zapytania o dane WordPress
Zapytania o dane WordPressBloki

Bloki

Przeczytaj więcej w przewodniku Praca z blokami (Gutenberg).

Oto przykłady queries do pobierania danych bloków.

Pobieranie bloków z custom posta za pomocą typu Block

Pobieranie danych wszystkich bloków w poście:

{
  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
              }
            }
          }
        }
      }
    }
  }
}

Pobieranie tylko bloków określonych typów:

{
  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
              }
            }
          }
        }
      }
    }
  }
}

Wykluczanie bloków:

{
  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
              }
            }
          }
        }
      }
    }
  }
}

Pobieranie danych bloków z custom posta za pomocą typu JSONObject

Pobieranie danych wszystkich bloków w poście:

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

Pobieranie tylko bloków określonych typów:

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

Wykluczanie bloków:

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

Pobieranie spłaszczonych danych bloków z custom posta

Pole blockFlattenedDataItems spłaszcza hierarchię bloków zawartą w custom poście do jednego poziomu. Dzięki temu filtrowanie według typu bloku obejmie również bloki wewnętrzne, których blok nadrzędny jest wykluczony.

Pobieranie danych wszystkich bloków w poście:

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

Pobieranie tylko bloków określonych typów:

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