Query products by metafield and collection id doesn't work

Hi everyone,

I’m trying to query products by metafield value and collection id using GraphQL API but it seems doesn’t work.

Do you guy have any idea?

Thanks a lot

Update your query to use double quotes for the value (and escape them in the string) e.g.

query {
  products(first: 50, query: "collection_id:6110285013 AND metafields.Sapp.optimizeState:\\\"optimized\\\"") {
    nodes {
      id
      title
    }
  }
}

If this doesn’t work remove the collection filter. If this returns products, but the combined query doesn’t, those products aren’t in the collection.

Hi @surfcoastdigi

Thanks for your reply.

It seems doesn’t work too.

But I’m pretty sure those products are in that collection. Because as you can see on my first screenshot, when I tried to fetch products (along with metafield value) belong to that collections, the API returns data correctly

I see. Try looser matching instead of AND use OR. See what that gives you.

Yep. It works with “OR”

Do you have any ideas why it doen’t work with “AND”?

1 Like

you should run isolated tests for either side, just collection and just metafield queries to confirm. It’s a very common outcome when combining filters with AND (whether implicit via space or explicit). Shopify’s search just returns the intersection of the sets.

Should probably report/ask at https://community.shopify.dev/

Looks like the query parser is failing with collection_id in compound queries-- what do you have in your “parsed” response?

This is parsed properly: