Hi there,
Has anyone experienced issues with the products by metafield value query found on Query by metafield value?
query Products {
products(first: 20, query: "metafields.custom.my_metafield_key:anything") {
edges {
node {
id
metafield(namespace: "custom", key: "my_metafield_key") {
id
value
}
}
}
}
}
The above query seems to return the same results regardless of what I try, for example:
"metafields.custom.my_metafield_key:*"
"metafields.custom.my_metafield_key:anything"
Even the example query provided in the above link returns results when the metafield does not exist at all in any of my products.
{
"data": {
"products": {
"edges": [
{
"node": {
"id": "gid://shopify/Product/14747395391820",
"metafield": null
}
},
{
"node": {
"id": "gid://shopify/Product/14776780685644",
"metafield": {
"id": "gid://shopify/Metafield/176692379746636",
"value": "Value"
}
}
}
]
}
},
"extensions": {
"cost": {
"requestedQueryCost": 12,
"actualQueryCost": 12,
"throttleStatus": {
"maximumAvailable": 2000,
"currentlyAvailable": 1988,
"restoreRate": 100
}
}
}
}
1 Like
Does this return same results?
products(first: 20, query: "metafields.custom.my_metafield_key:\"anything\"") {
Please don’t forget to wrap \"
+ your value + \"
.
Hey @gRoberts !
In addition to the syntax mentioned by Remy, another issue may be that your metafield search isn’t filtering results because you may not have “filtering enabled” turned on in your metafield definition. I tested this out myself and when I search for a metafield that doesn’t have filtering enabled, it will be ignored in the query, returning all results (like you are seeing). Once I enabled filtering for the metafield definition I was querying, it was then returning properly.
I just want to note as well since I use this almost every day; using debug headers is a great way to troubleshoot search queries! They show exactly how Shopify is parsing your query, which can be super helpful when diagnosing issues like this. If you’re interested in learning more about this tool, check out our search syntax debugging docs.
Thanks both,
Seems that hasn’t had any effect on the results, even when trying to use
query: "metafields.custom.my_metafield_key:\"{*\""
or
query: "metafields.custom.my_metafield_key:{*"
Debug returns
[
{
path: [ 'products' ],
query: 'metafields.custom.my_metafield_key:{*',
parsed: { field: 'metafields.custom.my_metafield_key', match_prefix: '{' }
}
]
Yet it still returns results where that metafield does not exist or is empty.
Apologies for not mentioning this before but the metafield is a JSON field.
An app we used to use stored data in a metafield as JSON and we need to process it but we have hundreds of thousands of products and not all of them have this field so I don’t want to process ALL of them unless absolutely necessary.
Hey @gRoberts ,
Thanks for the update. Since you mentioned this is from an app you’ve used, there could possibly be some permissions in place blocking this from being returned.
Since this looks like a one time migration, another route could be to do this through the metafieldDefinition. Here’s an example query that returns the products associated with a metafield.
query MetafieldDefinition {
metafieldDefinition(id: "gid://shopify/MetafieldDefinition/123456789") {
metafields(first: 10) {
nodes {
owner {
metafield(namespace: "custom", key: "test") {
id
owner {
... on Product {
id
}
}
}
}
}
}
}
}
Thank you @KyleG-Shopify
This is exactly what I needed!
1 Like