How do you handle nested taxonomies?

default discord avatar
thgh
7 months ago
8

Wordpress has this pretty well covered.

  • discord user avatar
    jacobsfletch
    Payload Team
    6 months ago

    Enable the nested docs plugin

    https://github.com/payloadcms/plugin-nested-docs

    on a

    categories

    collection then use a relationship field to reference the category



    Here's an example

    categories

    collection


    {
        slug: 'categories',
        name: 'Categories',
        fields: [
            {
                name: 'name',
                type: 'text',
            }
        ]
    }


    Then in your Payload config, call the plugin



    import nestedDocs from '@payloadcms/plugin-nested-docs'
    
    export default buildConfig({
      ...
      nestedDocs({
        collections: ['categories'],
      }),
    })


    Then in a

    posts

    collection, use a relationship field



    {
        slug: 'posts',
        name: 'Posts',
        fields: [
            {
                name: 'title',
                type: 'text',
            },
            {
                name: 'categories',
                type: 'relationship',
                hasMany: true,
                relationTo: 'categories',
            }
        ]
    }


    This would enable something like

    News > Politics > United States
  • default discord avatar
    thgh
    6 months ago

    Oh lol, I thought docs stood for documentation, unfortunate naming šŸ˜›



    Thanks, will check it out!

  • discord user avatar
    jacobsfletch
    Payload Team
    6 months ago

    Ahh makes sense. The original concept was actually ā€œnested pagesā€ but since this pattern applies to collections of any type ā€œnested docsā€ made the most sense

Open the post
Continue the discussion in Discord
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.