Page not found with custom post types

Just a quick post on a very frustrating problem I was having with WordPress and the new custom post types introduced in version 3.0. Everything was rolling along smoothly until it came to actually viewing one of the custom posts… 404 error. Weird, maybe a .htaccess issue… nope that’s all good, spelling error.. nope. Long story short, this tinkering went on for a good couple of hours and the problem wouldn’t go away…. cry! I tried everything I could think of but nothing helped. I could view the post pages with permalinks turned off, but it failed with them turned on.

After hours of frustration I stumbled upon a similar forum post with the answer:

1
flush_rewrite_rules( false );

Hours of frustration over such a simple fix. Sods law I guess. Add this code to the function you use to register your custom post type like so (usually in your themes functions.php):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function post_type_myposttype() {
    $labels = array(
        /* Labels here */
    );

    register_post_type(
        'myposttype',
            array(
                'labels' => $labels,
                'singular_label' => __('My Post Type'),
                /* More settings etc */
                'rewrite' => array('slug' => 'custom-post-slug'),
                'query_var' => false,
                'supports' => array(
                    'title',
                    'editor',
                    'author',
                    'thumbnail',
                    'excerpt',
                    'custom-fields',
                    'revisions')
    ));
    /* IMPORTIONT: Remember this line! */
    flush_rewrite_rules( false );/* Please read "Update 2" before adding this line */
}
//Initialise custom post type
add_action('init', 'post_type_myposttype');

No more 404 errors! Huzzah! One last tip: make sure you turn off any caching plug-ins (DB-Cache reloaded, Hypercache etc) before you start redeveloping your WordPress blog. Yes I made that mistake too! Yesterday evening is one I wish to forget…

Update: If you are adding custom taxonomies to your post types you may need to add the flush rewrite rules to the function when you initialise them:

1
2
3
4
5
6
function create_custom_taxonomies(){
    register_taxonomy('taxonomy1', 'posttypename', array( 'hierarchical' => true, 'label' => 'Taxonomy1'));
    register_taxonomy('taxonomy2', 'posttypename', array( 'hierarchical' => true, 'label' => 'Taxonomy2'));
    flush_rewrite_rules( false );/* Please read "Update 2" before adding this line */
}
add_action('init', 'create_custom_taxonomies' );

Update 2: Adding flush_rewrite_rules in the places mentioned above will force a flush for every page load. This is bad! For a better solution see Kens comment below.

Add your register_post_type to init, then add a function to with register_activation_hook that itself adds an action to init (priority 11) that in turn flushes the rewrite. This way you don’t flush on every single page load (which is bad).

Alternatively you could add the flush_rewrite_rules where suggested and reload your page; this will flush the rewrite rules and fix the 404 errors. Remember to remove or comment out the flush_rewrite_rules line after as it’s no longer needed! Thanks Ken for this update.