I added the following code to my functions.php file to create a custom post type.
function create_post_type() {
register_post_type( 'mysite_reviews',
array(
'labels' => array(
'name' => __( 'Reviews' ),
'singular_name' => __( 'Review' )
),
'public' => true,
'menu_position' => 5,
'rewrite' => array('slug' => 'reviews')
)
);
}
add_action( 'init', 'create_post_type' );
I then created a copy of my single.php file named single-mysite_reviews.php.
I went to my WordPress dashboard and the custom post type was showing up in my menu. I created a new post from the new menu and published it. When I tried to view this new post all I could get was an error. Or more specifically, this page: http://gointrigue.com/vanilla/reviews/my-site-reviews-custom-post-type/
What am I doing wrong?
2 Answers
flush_rewrite_rules()
as soon as you register the custom post type. It appears that they have not been rewritten yet, that is, most probably, why you’re getting an embarrassing message.
register_post_type( 'mysite_reviews',
array(
'labels' => array(
'name' => __( 'Reviews' ),
'singular_name' => __( 'Review' )
),
'public' => true,
'menu_position' => 5,
'rewrite' => array('slug' => 'reviews')
)
);
flush_rewrite_rules(); // <- do this only once!
Alternatively you can go to Settings -> Permalinks -> Save changes, which calls on flush_rewrite_rules()
for you.
In fact http://gointrigue.com/vanilla/?post_type=mysite_reviews shows your post, which means that it’s just not being rewritten due to the lack of the proper rules, which have not yet been flushed to the database.