[wp-trac] [WordPress Trac] #55880: Custom post type isn't redirected to its canonical URL
WordPress Trac
noreply at wordpress.org
Tue May 31 11:54:44 UTC 2022
#55880: Custom post type isn't redirected to its canonical URL
--------------------------+-----------------------------
Reporter: manooweb | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Canonical | Version:
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
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
<?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');
}}}
3. Create a custom taxonomy term: Custom term example (slug: custom-term-
example)
4. Create a custom post: Custom post example (slug: custom-post-example)
and link it to Custom term example
5. 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/ 👌
6. 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
7. 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
8. 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
--
Ticket URL: <https://core.trac.wordpress.org/ticket/55880>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list