WordPress-gegevens opvragen
WordPress-gegevens opvragenAangepaste berichten

Aangepaste berichten

Lees meer in de gids Werken met aangepaste berichten.

Dit zijn voorbeelden van queries om gegevens van aangepaste berichten op te halen.

CPT's gekoppeld aan het schema

Haal aangepaste berichten op met CPT's "post" en "page":

query {
  customPosts(filter: { customPostTypes: ["post", "page"] }) {
    ...CustomPostProps
    ...PostProps
    ...PageProps
  }
}
 
fragment CustomPostProps on CustomPost {
  __typename
  title
  excerpt
  url
  dateStr(format: "d/m/Y")
}
 
fragment PostProps on Post {
  tags {
    id
    name
  }
}
 
fragment PageProps on Page {
  author {
    id
    name
  }
}

CPT's niet gekoppeld aan het schema

Haal aangepaste berichten op voor verschillende CPT's (die ingeschakeld moeten zijn om via Instellingen bevraagbaar te zijn):

query {
  customPosts(
    filter:{
      customPostTypes: [
        "page",
        "nav_menu_item",
        "wp_block",
        "wp_global_styles"
      ]
    }
  ) {
    ... on CustomPost {
      id
      title
      customPostType
      status
    }
    __typename
  }
}

CPT's filteren op een aangepaste taxonomie

Haal aangepaste berichten op gefilterd op categorie:

query {
  customPosts(
    filter: {
      categories: {
        includeBy: {
          ids: [26, 28]
        }
        taxonomy: "product-cat"
      }
    }
  ) {
    ... on CustomPost {
      id
      title
    }
    ... on GenericCustomPost {
      categories(taxonomy: "product-cat") {
        id
      }
    }
  }
}

Aangepaste berichten aanmaken

Om CPT's aan te maken die geen extra velden vereisen bovenop die van een Post, kun je de mutatie createCustomPost gebruiken.

Deze query maakt een item aan voor de CPT "my-portfolio":

mutation {
  createCustomPost(
    input: {
      customPostType: "my-portfolio"
      title: "My photograph"
      contentAs: { html: "This is my photo, check it out." }
    }
  ) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
      ...on GenericErrorPayload {
        code
      }
    }
    customPost {
      __typename
      ...on CustomPost {
        id
        title
        content
      }
    }
  }
}

Aangepaste berichten bijwerken

Deze query werkt de titel en inhoud bij voor de CPT "my-portfolio":

mutation {
  updateCustomPost(input: {
    id: 1
    customPostType: "my-portfolio"
    title: "Updated title"
    contentAs: { html: "Updated content" }
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    customPost {
      __typename
      ...on CustomPost {
        id
        title
        content
      }
    }
  }
}

Of met geneste mutaties:

mutation {
  customPost(by: { id: 1 }, customPostTypes: "my-portfolio") {
    originalTitle: title
    update(input: {
      title: "This is my new title",
      contentAs: { html: "This rocks!" }
    }) {
      status
      errors {
        __typename
        ...on ErrorPayload {
          message
        }
      }
      customPost {
        __typename
        ...on CustomPost {
          id
          newTitle: title
          content
        }
      }
    }
  }
}