Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Search on a localized field (i.e.: slug) with fallback

default discord avatar
nomad.devlast year
2

Hello everyone, I hope you're fine! 🤗



I'm currently struggling to understand if it's an expected behavior or not. I have a Product collection including a slug (which is generated based on the name of the product).


I wanted to have localized slugs to match the name in the user's language. But if the content is not set in one language I was expecting to be able to find the product anyway with the slug in the fallback language.



Is that something that should work?



  const product = await payload.find({
    collection: 'products',
    locale: 'fr',
    fallbackLocale: 'en',
    where: { slug: { equals: slug } },
    limit: 1,
  });


Or is that not really logical? How would you manage this case?


Thanks

A LOT!

🙏

  • default discord avatar
    ritsu0455last year

    Nope!

    fallbackLocale

    isn't really used in querying, only for the result transformation.


    I would have an array field like

    slugs

    with

    locale

    and

    value

    properties, with it you would be more free on querying. You should synchronize it with hooks with your

    slug

    .

  • default discord avatar
    nomad.devlast year

    Ouuuuh, smart and elegant! Indeed, I didn't think it this way!


    Thanks for the confirmation and the nice solution!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.