Query all products with a given metafield value

I am working a shopify app where I want to know the “status” of each product, which is either “complete” or “needing review”. My app needs to pull all “needing review” products into the interface and show them in a paginated ResourceList.

In order to pull all products with the status “needing review”, I believe I two options:

  1. Query from Shopify all products with the status “needing review”

  2. Record in my own database the list of product ids that need review, and filter my queried products by those ids.

I’ve been trying option (2.) however its extremely messy to simultaneously support pagination and search while also filtering queried products with a list of ids. For example, I will read 50 products for a page, but then only 35 will have the correct status, so then I end up with each of my pages having a different number of products.

Option (1) would be much easier if I am able to query Shopify products by what status I have given the product. However it seems like the only place in a product I can add custom information is in the metafields. From looking at this post https://community.shopify.com/c/Technical-Q-A/Querying-productvariants-by-metafields-in-GraphQL/m-p/572305/highlight/false#M11227 and the QueryRoot documentation (https://help.shopify.com/en/api/graphql-admin-api/reference/queryroot) it doesn’t seem like its possible to query by metafield value.

Basically, I’m really confused about how I can query products by a custom field.

7 Likes

Unfortunately, there is no way to query products by metafield value or any other “custom” field value.

1 Like

Need this as well… one of the main reasons we want to use private meta-fields for our app would be so that we can retrieve a list of products that we have tagged. Having to sync that with another DB just because the meta fields aren’t queryable is somewhat cumbersome.

Is adding a query option for (private) meta-fields something that would be considered to be added to the API? It would already be sufficient if it’s as simple as being able to query for a key name.

Example of what it could look like to query all products that have a private meta-field with key “status”:

query getProductsWithMetaField {
  products(first: 5, query: "private_meta_field:status") {
    edges {
      cursor
    }
  }
}
8 Likes

Thank you so much for your response! What is the recommended/standard way to add information about each product in a queryable way? I imagine it’s fairly common for apps to need to query products by some information that they added to each product. Is the only solution to copy everything into a separate database to made the data queryable?

If the solution is a separate database, how do we manage syncing the database to ensure its up to date with all the merchant’s products? Will web hooks capture all updates to the products?

Thank you!

4 Likes

Unfortunately, yes. As to the ways to ensure all updates are caught, you’d use webhooks + scheduled Products REST API with updated_at_min filter to do reconciliation on possible missed updates.

1 Like

use tags

1 Like

use tags

Unfortunately tags are public-facing, e. g. the shop customer will be able to filter the catalog by tags. Not really a good place to store app-specific data.

2 Likes

This is incredibly sad to hear. I have been thinking about another possible work around. In case those reading are interested.

The idea is to download an entire list (of products for example) from Shopify. Then write some custom code to loop through each one to find a match. So basically download the entire Shopify list to your server for the purpose of finding just one record.

It is incredibly inefficient. Not recommended if you’re trying to built a customer facing search feature. But in my case I wish to sync a local data source with Shopify via something other than the Shopify ID. The script itself will be run automatically over night, or manually by a developer. So the lack of efficiency won’t really matter in this case.

Any update to this given the recent Unite announcements related to metafields?

This would be a great feature to have. The current workaround seems pretty cumbersome, having to download your entire product database on an automatic and frequent basis, as per https://www.shopify.com/partners/blog/shopify-metafields

A very costly operation on both ends, IMO.

4 Likes

Using the new metafields options would be so much more of value if one can query orders, product etc. by meta fields

11 Likes

I also have a use case for this. Shopify needs to add this!

6 Likes

Agreed, not having this just makes for cumbersome error prone solutions.

2 Likes

How is this still not a thing? The only way you can return an array of products in Liquid is using an internal dummy collection?

1 Like

I’m pretty sure this topic was created before the metafield system had been revamped. I don’t have an updated answer, but it might be worth checking again. Most likely, only the GraphQL version of the API would support this, if anything does.

Just throwing an idea in the mix:

can a specific Tag be assigned to a product if that product has a certain metafield value?
And then use that tag for queries.
Adding tags to hundreds of products according to already assigned metafield values could be a task too big for some.

I’ve been researching this and so far the answer seems to be “not without API access” aka you have to find/subscribe to/custom-develop an app that can create that flow. Such as alloy automation or arigato or Shopify Flow (only available for the more expensive Shopify plans).

I’m currently trying to learn how to develop my own custom app to do that, because I have seriously EVERYTHING else I need automated.

Is this what you are all looking for? You will need the filtering enabled in the Shopify Search & Filter app.

query tumbleDryProducts {
collection(handle: “filterable-collection”) {
handle
products(first: 1,
filters: {
productMetafield:{
namespace:“product_care”,
key:“drying_instructions”,
value:“tumble dry”
}
}) {
edges {
node {
id
title
}
}
}
}
}

There is a reference to doing this on the Shopify docs: https://shopify.dev/api/examples/filter-products#query-products-by-metafield-value

However, I’m having some issues getting it working and have opened a discussion on Github:
https://github.com/Shopify/storefront-api-feedback/discussions/139

2 Likes

I remember trying something like that and getting bizarre results as well. I just assumed that the current shopify API doesnt support this GraphQL syntax for metafields.

[edit] Thanks for putting in that bug report!

1 Like

This is no longer true the following code works:

query Diamonds {

collection(handle: “best-selling-diamonds”) {

handle

products(first: 5, filters:

{ productMetafield: {

namespace:“diamond”, key:“shape”, value:“OVAL” } })

{

edges {

node {

handle

title

metafield: metafield(namespace: “diamond”, key:“shape”){

value

description

type

}

}

}

}

}

}

You need to make sure you’re namespaces are all lowercase, the metafield is exposed to the storefront API, and it is enabled is the Shopify filtering app. If you need any more help [email removed] me.

2 Likes