Get an item from the CMS based on a specific field

Hi!
I’m working on integrating a specific API to the CMS that creates Items. On occasions, I will need to update an item. I’m using the Webflow API of course.
I’m trying to figure out if there is a way to search through the items in a collection and get an item based on a specific field value. Kind of like querying for posts by meta value in WP. Are there any ideas or workarounds available?
Thanks in advance.

Hi @Gaia have a look at this … all available in the API docs… is this what you are looking for?

Thanks, but I know the docs :slight_smile: unless Im missing something I did not find a way to get an item based on a field different than the ID

One year later, still no possibility to search a CMS Item by custom field ?

Any update on this? @Gaia did you find a work around?

Lookup tables…

My typical approach has been to backup the CMS into Airtable and then via Integromat (or Zapier) you can search by field values in Airtable. Each row in Airtable has the CMS Item ID so if you need to reference that CMS Item you know can :grinning_face_with_smiling_eyes:

1 Like

@ChrisDrit does Airtable automatically populate the CMS Item ID? That would be awesome, but I’ve only ever seen tutorials where designer has to go into the CMS list and manually input a field for CMS Item ID. Haven’t actually used Airtable but would hop in if it does that. Thanks!

Gosh no, nothing manual :grinning_face_with_smiling_eyes:

You can setup a Webhook that sends any newly added/edited/deleted CMS item into Airtable automatically.

Here’s a tutorial I wrote walking through how to do this (step-by-step):

Happy to answer any questions here…

1 Like

@ChrisDrit Wow. What a great writeup. Wish I had found this 3 weeks ago when I ended up building my own Webhook Server because I couldn’t figure out why Integromat wouldn’t allow me to select a trigger other than “Form Submission.” :joy:

1 Like

:pray: sorry you didn’t see it then but good to hear it’s open up some doors for you :slightly_smiling_face:

Little old answer (But maybe helpful for someone).

By lodash ( A modern JavaScript utility library delivering modularity, performance & extras.) it is very easy to get this idea.

Lets say this is your API Request:

GET /collections/:collection_id/items

Example JSON response

 {
      "items": [
        {
          /* cms item 1 */
          "_archived": false,
          "_draft": false,
          "featured": false,
          "name": "Article-1",
          "slug": "article_1",
          "_cid": "580e63fc8c9a982ac9b8b745",
          "_id": "580e64008c9a982ac9b8b754"
        },
        {
          /* cms item 2 */
          "_archived": false,
          "_draft": false,
          "featured": false,
          "name": "Article 2",
          "slug": "article_2",
          "_cid": "480e63fc8c9a982ac9b8b745",
          "_id": "580e64008c9a982ac9b8b754"
        }
      ],
      "count": 1,
      "limit": 1,
      "offset": 0,
      "total": 5
    }

Now lets say I want to get only the item with "name: Article-1 + _archived': false (Or any combination you want).

By FILTER function it is very easy (Useful for front-end filtering of API calls).

https://lodash.com/docs/4.17.15#filter

  const filter = _.filter(posts[0].items, { 'name': "Article-1", '_archived': false });
  /* return Article-1 */

Full code example (Try inside Codepen):

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

<script>
  /* Example JSON response for:
  GET /collections/:collection_id/items
  */
  const posts = [
    {
      "items": [
        {
          /* cms item 1 */
          "_archived": false,
          "_draft": false,
          "featured": false,
          "name": "Article-1",
          "slug": "article_1",
          "_cid": "580e63fc8c9a982ac9b8b745",
          "_id": "580e64008c9a982ac9b8b754"
        },
        {
          /* cms item 2 */
          "_archived": false,
          "_draft": false,
          "featured": false,
          "name": "Article 2",
          "slug": "article_2",
          "_cid": "480e63fc8c9a982ac9b8b745",
          "_id": "580e64008c9a982ac9b8b754"
        }
      ],
      "count": 1,
      "limit": 1,
      "offset": 0,
      "total": 5
    }
  ];
  
  // The `_.matches` iteratee shorthand.
  const filter = _.filter(posts[0].items, { 'name': "Article-1", '_archived': false });
  console.log(filter);
</script> 

When this idea is also useful?

In general for any API.

If the API do not support filtering by category, country, and so on - this idea give you an option to filter the data inside webflow custom code.

image