custom post types – no permalinks or 404
-
i am at my wits end on this. i am simply trying to get simple posts – of a custom post type – to show up on the front end of the site with a pretty url. i have all the basics (.htaccess, permalink settings, etc). i am not new to wordpress, but this is the first time i am using custom post types. please bear with me, i will try to provide lots of info.
i have my permalink settings set to use custom structure of “/%category%/%postname%/”.
i installed a brand new copy of wordpress (4.9.4). no plugins enabled. i created 50 categories, one for each state in the u.s. the slugs for those are the state name, so alaska permalink is https://mysite.com/alaska. all good.
i installed a super-simple custom plugin that does nothing but create a custom post type (city). i will paste that code below.
i create a post of type city (say, “anchorage”), with the slug “anchorage-ak”. by default, when i edit the post in wp-admin, the permalink for the post is:
https://mysite.com/city/anchorage-ak.i understand that is the default behavior for custom post types, but i need the url to be:
https://mysite.com/alaska/anchorage-akso, i added a rewrite option to the plugin’s call to register_post_type() like so:
‘rewrite’ => array(‘slug’ => ‘%category%’),
when i edit the post now, there is literally NO PERMALINK that shows below the post title. the label “Permalink:” is there, followed by the “edit” button.
so, i installed a permalink plugin “permalink manager lite”. (i have tried several, that one seems to come the closest to doing what i want.) i activated that plugin, and set the permastructure for city posts to “%category%/%postname%”. when i edit the post now, i see the permalink i want:
https://mysite.com/alaska/anchorage-akwhen i try to access that url from the front end – i type it into the browser address bar – the url changes (a redirection that i don’t understand) to:
https://mysite.com/%category%/anchorage-ak/and of course i get a 404. i get the same result if i use this url instead
https://mysite.com/city/anchorage-ak (redirect and 404)if i remove the
‘rewrite’ => array(‘slug’ => ‘%category%’),from the register_post_type() call, the permalink goes back to
https://mysite.com/city/anchorage-ak.and if i disable the plugin, when i edit the post there is no permalink.
UPDATE: when i turn off the permalink manager lite setting for “canonical redirect”, then the redirect to urls including “%category%” goes away. instead, if i include the “rewrite” stuff in the register_post_type() call, there is no redirect, and i get a 404 on the url https://mysite.com/alaska/anchorage-ak. if i remove the redirect stuff from the register_post_type() call, then the permalink goes back to https://mysite.com/city/anchorage-ak.
i know i must be missing something obvious. it can’t be that difficult to get the permalinks i want. or can it?
can anyone help?
thanks VERY much in advance.
—
here is the code for the plugin:add_action( ‘init’, ‘create_post_types’ );
function create_post_types()
{
register_post_type( ‘city’,
array(
‘labels’ => array(
‘name’ => __( ‘Cities’ ),
‘singular_name’ => __( ‘City’ )
),
‘description’ => __(‘City Posts represent a single city within a given state’),
‘public’ => true,
‘has_archive’ => false,
‘rewrite’ => array(‘slug’ => ‘%category%’),
‘menu_position’ => 5,
‘hierarchical’ => false,
‘supports’ => array(‘title’, ‘editor’, ‘author’, ‘trackbacks’, ‘custom_fields’, ‘comments’),
‘taxonomies’ => array(‘category’, ‘post_tag’),
)
);
flush_rewrite_rules();
}
- The topic ‘custom post types – no permalinks or 404’ is closed to new replies.