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

default discord avatar
ahmetskilinclast 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?

  • Selected Answer
    discord user avatar
    jmikrut
    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
    ahmetskilinclast year

    Thank you!

  • default discord avatar
    ahmetskilinclast year

    Hey @jmikrut how about with an array of tags?

    Post: {
      /*...*/
      title,
      tags: Tag[],
      /*...*/
    }
    
    Tag: {
      id,
      name,
    }
    
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.