Rest API filtering

default discord avatar
.kilosierralast year
2

My posts collections includes tags as follows



 {
      name: 'tags',
      type: 'relationship',
      relationTo: 'tags',
      hasMany: true,
    },

I want to be able to filter the response by tag name. I also want to place a limit of 6 so I don't break my pagination, etc.



Given a tag of "Guides", shouldn't I be able to filter my posts using



https://domain.com/api/posts?where[tags][equals]=Guides&limit=6&page=1


The above url params don't work. I've attached a screenshot of the api response.



I'm using this url based on my interpretation of the following documentation -

https://payloadcms.com/docs/queries/overview#rest-queries

What am I doing wrong?

  • default discord avatar
    svn__last year

    Hey, I see that you are using

    hasMany: true

    , so you should try with something like

    ?where[tags.name][equals]=tagname

    where name is a property from Tags collection



    You can find more details in this section:

    https://payloadcms.com/docs/fields/relationship#how-the-data-is-saved

    , and more specifically under "Has Many - Polymorphic"

  • default discord avatar
    .kilosierralast year

    That worked perfectly. Thanks

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.