Hi everyone,
I’m having some trouble automating the initialization of metafields for pages in Shopify, and I could really use your help.
The Situation: I’m trying to add seo metafields (like “Title” and “Description”) to a page using Shopify’s GraphQL API. I want to do this automatically without manually entering values through the UI.
The Problem: Whenever I create a new page and leave the metafield values empty, these metafields simply don’t appear. It seems like they have to be filled with something initially to even be visible later on.
Currently, I end up going through the Shopify interface, manually inputting random values for the metafields, and saving the changes, just so I can see them. Obviously, this is not ideal, as I’d prefer to have this process be fully automated.
What I Want: I’m trying to find a way to automatically initialize these metafields whenever a new page is created, even if the values are default or random. Ideally, I’d like a solution where, upon page creation, these metafields are filled with default values so I don’t have to manually do it every time.
My Questions:
- Why are the metafields invisible if no value is initially provided? Is this a limitation or behavior within Shopify?
- Is there a way to automatically set a default value for metafields upon creating a page using GraphQL or another method?
Any guidance or suggestions on how to solve this would be greatly appreciated. Thanks so much in advance for your help!
My code … :
mutation {
metafieldsSet(metafields: [
{
key: "title",
namespace: "custom",
ownerId: "gid://shopify/Page/<PAGE_ID>",
type: "single_line_text_field",
value: "Your Custom Title"
},
{
key: "description",
namespace: "custom",
ownerId: "gid://shopify/Page/<PAGE_ID>",
type: "multi_line_text_field",
value: "Your custom description here."
}
]) {
metafields {
id
key
namespace
value
}
userErrors {
field
message
}
}
}