I'm adding a custom post type like so (names have been altered to protect the innocent):
add_action('init', 'my_post_types');
function my_post_types() {
register_post_type('publication',
array(
'labels' => array(...),
'rewrite' => array(
'slug' => 'publications/%year%',
'with_front' => true
),
'public' => true,
'menu_position' => 21,
'supports' => array('title', 'editor', 'author', 'excerpts', 'revisions', 'thumbnail'),
'show_ui' => true,
));
}
I would expect with these settings that the URL of a given publication would be:
/publications/2012/name-of-publication
However, the URLs produced by get_permalink()
are coming out as:
/publication/name-of-publication
The desired URL pattern is recognised by WordPress, but simply redirects to the lower URL. This URL does display the desired page.
I've tried the commonly suggested solutions:
- Making sure that
register_post_type
is called on theinit
action, so it isn't created too late. - Flushing the rewrite rules by visiting the Permalinks settings page. I also tried doing this on every page using
$wp_rewrite->flush_rules()
, but that would be pointlessly expensive in reality. - Changing the value of
with_front
has no effect. I wouldn't expect it to since the site is at/
, not in a subfolder. - Taking out the
%year%
part of the slug has no effect either, so it isn't causing the rewrite rules to fail. - I don't have the Redirection plugin installed. It's a known troublemaker with URLs.
If necessary, I can intercept WordPress' routing and force it to deliver the right page, and use filters to return the right URL from get_permalink
, but that seems like the wrong solution. Setting the rewrite rules when I register the post type is the right solution. Is there something I'm doing wrong?