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?
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.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.
Not really sure what you want to achieve from your description but, if you want a custom post type in the loop, do this:
Custom post types require a bit more manual tuning when you use them. Not only do you need to enable them but also list them or add them to the loop, create an archive (or at least link to it).
PS: Code uses PHP 5.3+ Closures. Convert to PHP 5.2 yourself, as an assignment 🙂