issues with cpt permalinks and 404s
-
this plugin appears to do exactly what i want. except…
i am simply trying to get 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 installed a brand new copy of wordpress (4.9.4). no plugins enabled. i have my permalink settings set to use custom structure of “/%category%/%postname%/”. 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 created a simple custom post type (city). i will paste that code below so you can see.
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 need the url to be: https://mysite.com/alaska/anchorage-ak
so, 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 this 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-akbut when i try to access that url i get a 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.
so, why am i getting a 404 on a permalink that wordpress is displaying to me on the edit form?
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 ‘issues with cpt permalinks and 404s’ is closed to new replies.