Like what we’re doing? Star us on GitHub!

How do you handle nested taxonomies?

thgh
last month
8

Wordpress has this pretty well covered.

  • jacobsfletch
    Payload Team
    2 weeks 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
  • thgh
    last week

    Oh lol, I thought docs stood for documentation, unfortunate naming 😛



    Thanks, will check it out!

  • jacobsfletch
    Payload Team
    last week

    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
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More