Make WordPress Core

Opened 2 years ago

Last modified 12 months ago

#55880 new defect (bug)

Custom post type isn't redirected to its canonical URL

Reported by: manooweb's profile manooweb Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Canonical Keywords:
Focuses: Cc:


At the opposite of a custom taxonomy term which is redirected to its pretty URL when we use its plain permalink, a custom post type is not.

  1. Set a custom permalink structure in WordPress permalinks settings like /%postname%/
  2. Register a custom taxonomy and custom post type by using the code below for example
 * Plugin Name: custom taxonomy and custom post type for testing.

function test_custom_taxonomy_and_post_type() {

        $custom_tax_labels = array(
                'name'          => 'Custom taxonomies',
                'singular_name' => 'Custom taxonomy',
                'menu_name'     => 'Custom taxonomies',

                        'labels'       => $custom_tax_labels,
                        'public'       => true,
                        'rewrite'      => true,
                        'hierarchical' => true,

        $custom_post_labels = array(
                'name'          => 'Custom posts',
                'singular_name' => 'Custom post',
                'menu_name'     => 'Custom posts',

                        'labels'      => $custom_post_labels,
                        'public'      => true,
                        'rewrite'     => true,
                        'has_archive' => true,
                        'taxonomies'  => 'custom_tax',

add_action('init', 'test_custom_taxonomy_and_post_type');

  1. Create a custom taxonomy term: Custom term example (slug: custom-term-example)
  2. Create a custom post: Custom post example (slug: custom-post-example) and link it to Custom term example
  3. Try to get the plain permalink of the custom taxonomy We are redirected to the pretty URL 👌
  4. Do the same with the custom post example plain permalink URL We aren't redirected to its canonical URL which should be
  5. Notice that the <link rel="canonical" href="" /> is correctly set in the HTML source code by the wp_get_canonical_url() function
  6. Note that the custom post type archive page isn't redirected either. should be redirected to

Change History (1)

#1 @slamorte
12 months ago

Do you have a custom permalink with a string in Settings > Permalinks > Custom Structure? I just found a bug where adding a string like /news/%postname%/ breaks all core base redirects for pages, posts, and CPTs.

Note: See TracTickets for help on using tickets.