WordPress: new Custom Post Type returning 404

I added a custom post type to a WordPress website that I’m building and created two new posts using the custom post type. When I went to view them, I got a 404 error.

Create the custom post type:

add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'campus_advisory',
array(
'labels' => array(
'name' => __( 'Campus Advisories' ),
'singular_name' => __( 'Campus Advisory' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'advisory')
)
);
}

Did a bit of searching around and learned that after you create a custom post type, you need to visit the Permalinks page in the WordPress back-end (Settings > Permalinks). I clicked “Save” (even though I didn’t change anything on the page) and refreshed the custom post and it returned the post rather than a 404.

One step further, and I didn’t have to go this far, but folks that visited the Permalinks page and that didn’t fix their 404 error, they had to add
flush_rewrite_rules();
after calling register_post_type in their custom post type code snippet in functions.php (or custom_functions.php if you’re using the Thesis framework—the site I’m currently developing uses the Thesis framework).


Leave a Reply

Your email address will not be published. Required fields are marked *