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:

Description

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
<?php
/**
 * 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',
        );

        register_taxonomy(
                'custom_tax',
                'custom_post',
                array(
                        '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',
        );

        register_post_type(
                'custom_post',
                array(
                        '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 http://example.org/?custom_tax=custom-term-example. We are redirected to the pretty URL http://example.org/custom_tax/custom-term-example/ 👌
  4. Do the same with the custom post example plain permalink URL http://example.org/?custom_post=custom-post-example. We aren't redirected to its canonical URL which should be http://example.org/custom_post/custom-post-example
  5. Notice that the <link rel="canonical" href="" /> is correctly set in the HTML source code by the wp_get_canonical_url() function https://github.com/WordPress/WordPress/blob/6.0/wp-includes/link-template.php#L3913
  6. Note that the custom post type archive page isn't redirected either. http://example.org/?post_type=custom_post should be redirected to http://example.org/custom_post

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.