Opened 3 years ago
Last modified 19 months ago
#55880 new defect (bug)
Custom post type isn't redirected to its canonical URL
Reported by: |
|
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.
- Set a custom permalink structure in WordPress permalinks settings like /%postname%/
- 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');
- Create a custom taxonomy term: Custom term example (slug: custom-term-example)
- Create a custom post: Custom post example (slug: custom-post-example) and link it to Custom term example
- 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/ 👌
- 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
- Notice that the
<link rel="canonical" href="" />
is correctly set in the HTML source code by thewp_get_canonical_url()
function https://github.com/WordPress/WordPress/blob/6.0/wp-includes/link-template.php#L3913 - 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
Note: See
TracTickets for help on using
tickets.
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.