0

I have a custom post type called resources which has been registered as so:

register_post_type(
  'Resources',
  theme_build_post_args(
    // $slug, $singular, $plural
    'resources', 'Resource', 'Resources',
    array(
      'menu_position' => 20,
       'has_archive'    => true,
      //'has_archive'   => 'types',
      'public'          => true,
      'supports'            => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
      'taxonomies'      => array('topics', 'types', 'industries'),
      'rewrite' => array('slug' => 'resources/%types%')
    )
  )
);

This post type has three taxonomies which have been registered like this:

register_taxonomy(
  'topics',
  'topics',
  array(
    'hierarchical' => true,
    'label' => 'Topics',
    'query_var' => true,
    'show_admin_column' => true,
    'publicly_queryable' => true,
    // 'rewrite' => array('slug' => 'resources')
  )
);

register_taxonomy(
  'types',
  'types',
  array(
    'hierarchical' => true,
    'label' => 'Types',
    'query_var' => true,
    'show_admin_column' => true,
    'publicly_queryable' => true,
    // 'rewrite' => array('slug' => 'resources')
  )
);

register_taxonomy(
  'industries',
  'industries',
  array(
    'hierarchical' => true,
    'label' => 'Industry',
    'query_var' => true,
    'show_admin_column' => true,
    'publicly_queryable' => true,
    // 'rewrite' => array('slug' => 'resources')
  )
);

Currently, when accessing a category defined in any taxonomy, the slug ignores /resources. For example, I have a type called Article. When accessing this page, it sits on /types/article/ when I need it to sit on /resources/types/article/.

The same applies for topics and industries.

Now I have seen approaches stating this should do the trick:

'rewrite' => array('slug' => 'resources/%types%')

However, the above doesn't work for types, meaning types still do not sit under /resources, but also, I need this to occur for 3 taxonomies, rather than just 1 and I cannot rewrite slugs for 3 taxonomies.

How do I go about this?

6
  • Your question was not clear to me. "I need it to sit on /resources/types/article/" - are you trying to customize the permalink of the posts in your resources CPT, or is it the permalink of the terms in your taxonomy? (clicking on a term permalink would bring you to the term's archive which by default displays posts in that term) And for example the types taxonomy, do you want its terms to have the structure /resources/types/<term slug> (e.g. /resources/types/type-one), or do you want it be /resources/<term slug> (e.g. /resources/type-one) instead? I.e. No /types there.
    – Sally CJ
    Feb 16, 2023 at 14:33
  • @SallyCJ - The structure I'm after is /resources/types/<term slug>. For example, If I have a type called "Article", the structure I'm after is /resources/types/article. If I have an industry called "Accounting" the structure I'm after is /resources/industries/accounting. Lastly, if I have a topic called "Security", then the structure I'm after is /resources/topics/security
    – Freddy
    Feb 16, 2023 at 14:55
  • In that case, you just need to use 'rewrite' => array('slug' => 'resources/<taxonomy>'), e.g. 'rewrite' => array('slug' => 'resources/topics') for the topics taxonomy. Have you already tried that?
    – Sally CJ
    Feb 16, 2023 at 15:09
  • @SallyCJ - That's correct, I've already tried that for a single taxonomy, but the slug still remains as /topics/accounting/. Also, as mentioned above, I need this implemented for three different taxonomies, not just for one. As far as I can see rewrite is only possible for one taxonomy?
    – Freddy
    Feb 16, 2023 at 15:23
  • 1
    @SallyCJ - My permalink structure is just /%postname%/. I was testing 'rewrite' => array('slug' => 'resources/%types%') just to see if that would make types sit under /resources and if it was successful, was going to explore how to apply the same for the other 2 taxonomies. However, it didn't achieve what I was after. But, left the code in the demo to showcase what I've tried.
    – Freddy
    Feb 16, 2023 at 15:39

1 Answer 1

1

You commented, "As far as I can see rewrite is only possible for one taxonomy?".

So no, that's not true, it's possible for every taxonomy. Just remember that each taxonomy should have a unique rewrite slug, so that it does not conflict with other permalinks (for other taxonomies and for post types like post, page, etc.). So for instance, if the topics taxonomy uses topics as the rewrite slug, then your types and industries taxonomies need to use a different rewrite slug. (If you really must use/share the same slug, it's not impossible, but it's not in scope of this answer)

Now if these are the permalink structures that you want:

  • /resources/topics/<term slug> for the topics taxonomy
  • /resources/types/<term slug> for the types taxonomy
  • /resources/industries/<term slug> for the industries taxonomy

where an example permalink (URL) would look like this:

  • https://example.com/resources/topics/security/ for a term with the slug security, in the topics taxonomy
  • https://example.com/resources/types/article/ for a term with the slug article, in the types taxonomy
  • https://example.com/resources/industries/accounting/ for a term with the slug accounting, in the industries taxonomy

Then just set the rewrite slug for your taxonomy to resources/<taxonomy>, like so:

(this is the actual code I tried & tested working with WordPress v6.1.1)

function my_register_post_types() {
    register_post_type(
        'resources', // post type name/slug
        array(
            'public'  => true,
            'rewrite' => array( 'slug' => 'resources' ),
            'labels'  => array(
                'name' => 'Resources',
                // other labels
            ),
            // other args
        )
    );
}

function my_register_taxonomies() {
    register_taxonomy(
        'topics',    // taxonomy name/slug
        'resources', // attach to this post type
        array(
            'public'       => true,
            'rewrite'      => array( 'slug' => 'resources/topics' ),
            'label'        => 'Topics',
            'hierarchical' => true,
            // other args
        )
    );

    register_taxonomy(
        'types',     // taxonomy name/slug
        'resources', // attach to this post type
        array(
            'public'       => true,
            'rewrite'      => array( 'slug' => 'resources/types' ),
            'label'        => 'Types',
            'hierarchical' => true,
            // other args
        )
    );

    register_taxonomy(
        'industries', // taxonomy name/slug
        'resources',  // attach to this post type
        array(
            'public'       => true,
            'rewrite'      => array( 'slug' => 'resources/industries' ),
            'label'        => 'Industries',
            'hierarchical' => true,
            // other args
        )
    );
}

However, you need to first register the taxonomies, and only after that, register your resources post type, if its rewrite slug is resources, which is the default value, or that the slug starts with resources/. Otherwise, your term permalinks would result in a 404 error page. :/

// Register the taxonomy first.
add_action( 'init', 'my_register_taxonomies' );

// Then the post type.
add_action( 'init', 'my_register_post_types' );

Also, be sure to flush the rewrite rules (i.e. re-save your permalinks), by simply visiting the Permalink Settings admin page.

4
  • 1
    Thanks for the descriptive breakdown, wasn't aware you could attach taxonomies to post types this way. Can confirm your solution worked for me. Many thanks again! :)
    – Freddy
    Feb 16, 2023 at 18:58
  • Glad it worked for you, and you might want to set with_front to false, in case the permalink structure (the one you set via the "Permalink structure" field on the Permalink Settings page) for the post type would have a "front", i.e. a prefix like blog/ as in /blog/%postname%/. Because by default, that "front" will be added to taxonomy and CPT permalinks, unless with_front is explicitly set to false.
    – Sally CJ
    Feb 17, 2023 at 4:19
  • 1
    Additionally, note that the 1st parameter for register_post_type() is a post type name/*slug*, so always use a slug and not the post type's label - I see you use the uppercase R as in Resources, so that should instead be resources, just like in my example 🙂
    – Sally CJ
    Feb 17, 2023 at 4:22
  • 1
    Sally CJ thanks so much for explaining the order of registration of custom type posts and taxonomies, save me, really, save me. Mar 28 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.