How do I get posts that have a certain category set to it?

default discord avatar
ahmetskilinc
last year
1 1

Currently I'm using QueryString like so:

const stringifiedQuery = qs.stringify(
  {
    where: {
      category: {
        name: {
          equals: req.query.name,
        },
      },
    },
  },
  { addQueryPrefix: true }
);

But I am still getting back all posts instead of the ones with a certain category.

Post: {
  /*...*/
  title,
  category: Category,
  /*...*/
}

Category: {
  id,
  name,
}

How would I write my QueryString so I only get back the posts with a certain category?

  • discord user avatar
    jmikrut
    Payload Team
    last year

    Hey @ahmetskilinc — here's how you'd do that:

    const stringifiedQuery = qs.stringify(
      {
        where: {
          'category.name': {
            equals: req.query.name,
          },
        },
      },
      { addQueryPrefix: true }
    );

    You have to use dot notation to query on nested relationship fields, but this should be golden!

    2 replies
  • default discord avatar
    ahmetskilinc
    last year

    Thank you!

  • default discord avatar
    ahmetskilinc
    last year

    Hey @jmikrut how about with an array of tags?

    Post: {
      /*...*/
      title,
      tags: Tag[],
      /*...*/
    }
    
    Tag: {
      id,
      name,
    }
    
Open the post
Continue the discussion in GitHub
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

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