Creating a Unified URL Structure for Custom Taxonomy and Post Type in WordPress

When building a custom WordPress website, structuring your URLs cleanly and consistently is vital for both user experience and SEO. A well-organized URL structure helps search engines crawl your website more effectively and ensures users can navigate intuitively.

If you are working with a custom post type and a custom taxonomy, like videos and video categories, you may want both to share the same URL structure. Below, we’ll discuss how to achieve this with a code snippet that harmonizes the URL structure for both post types and taxonomies.

Scenario: Custom Post Type and Custom Taxonomy for Videos

Imagine you have a custom post type called videos and a taxonomy called video_categories. By default, WordPress would generate separate URL structures for both. The videos post type would look something like:

While the video_categories taxonomy would look like:

/video_categories/sample-category/

 

But what if you want both to follow the same URL pattern, such as:

/videos/sample-video/ (for posts)

and

/videos/sample-category/ (for taxonomy terms)

 

In this blog, we’ll walk through the process of achieving this URL structure using a custom code solution.

Step-by-Step Guide to Unifying URL Structures

The following code snippet will allow you to unify the URL structure between the videos custom post type and video_categories custom taxonomy.

1. Register Custom Taxonomy: video_categories

The register_taxonomy() function creates the video_categories taxonomy and assigns it to the videos post type. Notice how the rewrite argument is configured to match the videos slug, making the URL for both taxonomy and post type follow the same pattern.

function my_register_taxonomy() {
    register_taxonomy( 'video_categories', 'videos', [
            'rewrite'      => [
                'slug'         => 'videos',
                'with_front'   => false,
                'hierarchical' => true,
            ],
            'hierarchical' => true,
            'labels'       => [
                'name'          => 'Video Categories',
                'singular_name' => 'Video Category',
            ],
            'show_in_rest' => true,
        ]
    );
}
add_action( 'init', 'my_register_taxonomy' );

  • slug: This is set to videos, meaning the taxonomy terms will use the same slug as the post type.
  • hierarchical: Allows for a category-like structure.
  • show_in_rest: Ensures compatibility with the WordPress REST API.

2. Register Custom Post Type: videos

Next, we register the custom post type videos. Again, the rewrite argument ensures that posts in this post type will use the /videos/ slug.

function my_register_post_type() {
    register_post_type( 'videos', [
            'rewrite'      => [
                'slug'       => 'videos',
                'with_front' => false,
            ],
            'has_archive'  => true,
            'hierarchical' => true,
            'public'       => true,
            'supports'     => [ 'title', 'editor','author', 'thumbnail', 'excerpt', 'revisions', 'post-formats',  ],
            'labels'       => [
                'name'          => 'Videos',
                'singular_name' => 'Video',
            ],
            'show_in_rest' => true,
        ]
    );
}
add_action( 'init', 'my_register_post_type' );

  • slug: Again, the slug is set to videos, matching the URL structure.
  • hierarchical: This allows the post type to support child posts if necessary.
  • supports: Defines the features supported by this custom post type, such as the title, editor, thumbnails, etc.

3. Parsing Requests for Unified URL Structure

The third function hooks into the parse_request action to manipulate the query vars and create a unified URL structure. Essentially, this function checks if the URL matches the videos slug and whether it is a taxonomy term. If the term does not exist, it treats it as a post in the videos post type.

function wp_parse_request( $wp ) {
    $taxonomy  = 'video_categories';        
    $post_type = 'videos'; 

    if ( preg_match( '#^' . preg_quote( 'videos', '#' ) . '/#', $wp->request ) &&
        isset( $wp->query_vars[ $taxonomy ] ) ) {
        $slug = $wp->query_vars[ $taxonomy ];
        $slug = ltrim( substr( $slug, strrpos( $slug, '/' ) ), '/' );

        if ( ! term_exists( $slug, $taxonomy ) ) {
            $wp->query_vars['name']       = $wp->query_vars[ $taxonomy ];
            $wp->query_vars['post_type']  = $post_type;
            $wp->query_vars[ $post_type ] = $wp->query_vars[ $taxonomy ];
            unset( $wp->query_vars[ $taxonomy ] );
        }
    }
}
add_action( 'parse_request', 'wp_parse_request' );

  • Request Parsing: This function checks if the URL matches the structure /videos/. If the request does not correspond to a valid taxonomy term, it adjusts the query to interpret it as a custom post of type videos.
  • Flexibility: It dynamically handles the routing for both post types and taxonomy terms under the same slug (/videos/), simplifying the structure.

Benefits of a Unified URL Structure

1. Improved SEO

Using a single, cohesive URL structure across your content types improves crawl efficiency for search engines like Google. The streamlined URL structure provides better clarity about your content hierarchy, boosting your rankings.

2. Better User Experience

A consistent URL structure simplifies navigation, as users can easily guess the format of related content. This reduces friction and enhances the overall experience on your website.

3. Content Categorization

The video_categories taxonomy helps categorize videos into meaningful sections, improving discoverability while keeping the URLs neat.

Conclusion

Combining a custom post type and taxonomy under the same URL structure can be incredibly beneficial for both SEO and user experience. With the code snippet provided, you can unify the URLs of your WordPress custom post types and taxonomies, creating a clean, consistent structure.

This solution works great for websites where videos or other custom content need better organization, especially if you aim to create a media-rich or video-heavy platform. By implementing this in your theme or custom plugin, you will have greater control over the architecture of your WordPress site.

Are you looking to implement this on your WordPress website? Try out the above steps and improve your URL structure today!

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.