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:

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):

function post_type_myposttype() {
    $labels = array(
        /* Labels here */
                'labels' => $labels,
                'singular_label' => __('My Post Type'),
                /* More settings etc */
                'rewrite' => array('slug' => 'custom-post-slug'),
                'query_var' => false,
                'supports' => array(
    /* 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:

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.

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.

felix on October 30 10 / 302 Permalink

Thank you very much, you saved me lots of time, it worked great.

owen on November 6 10 / 309 Permalink

u saved my day… thank a lot

Frankie Jarrett on November 13 10 / 316 Permalink

This is solved my major dilema these past few days! Thank you for posting :)

I will also note that it’s important that you place this AFTER the register_post_type call within the function…it will not work if placed before it!

Kudos and thanks again.


Matt on November 14 10 / 317 Permalink

Hi Frankie,
Glad you found it useful! Thanks the after register_post_type tip, duly noted :)

James on December 3 10 / 336 Permalink

Thanks a lot, almost got into frustrations but this tip helps me a lot :)

Duane on February 12 11 / 42 Permalink

Thanks so much. You saved my life :)

thenelzon on March 18 11 / 76 Permalink


Leonid on March 23 11 / 81 Permalink

Thanks alot!

vanhalstd on March 31 11 / 89 Permalink

thanks, clarified things!

GroovyCat on April 14 11 / 103 Permalink

this did the trick….Thanks!

Catzie on April 28 11 / 117 Permalink

Fixed my problem ♪ Thanks!

noussh on April 29 11 / 118 Permalink

thanks. This is not mentioned in the Codex page.

Ken on May 4 11 / 123 Permalink

Although this code flushes the rewrite rules every page load (not recommended), I’ve not yet found a better solution.

Ken on May 4 11 / 123 Permalink

nkuttler help with a sollution in the irc channel.

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).

Matt on May 4 11 / 123 Permalink

Hi Ken,

That’s great thanks for letting me know! I’ll try it out and update the code in the post accordingly. I was concerned with flushing the rewrite rules every time but it seemed to be the only solution that worked.

Ben on June 20 11 / 170 Permalink

Am I missing something? Isn’t register_activation_hook only for plugins? From what I can find, it does not fire when a theme is activated.

Amado Martinez on July 25 11 / 205 Permalink

Does anyone actually know what is the cause for the permalinks to fail? I’m glad I found the solution for this, but now I’m concerned about this happening on a live installation (I’m currently in production.)


Patrick Tang on August 27 11 / 238 Permalink

Thanks for the tip! Save me a lot of time and problems :)

Diego Reis on September 7 11 / 249 Permalink

My site don’t work wit this tip… work on old times e puft… don’t work any more!
I need help, i don’t know what to do now…. stay on live installation, the site is out dont show any custom post…. but list then on archives, and category….
access whith
url/post_type/category/post_name/?post_type=post_name why that?

sorry my bad english, you post is the unique whit this problem but not resolve meu particular problem

I use this code before this problem.

Raulghm on October 29 11 / 301 Permalink

Ohhhhhh thank a lot!!

dann on February 1 12 / 31 Permalink

life saver. thanks!

Bryan on March 24 12 / 83 Permalink

I’ve tried Ken’s solution and it doesn’t seem to work. I’ve seen him post this answer elsewhere. Ken, if you see this comment, a code example would be great! In my testing I discovered when I added an “init” action from the activation hook function, it never gets invoked for some reason.

I REALLY don’t want users of my plugin, which creates a CPT, to have to manually save the permalink settings, but it might come to that. I’ve been struggling with this for 2 days. :(

Michael on May 8 12 / 128 Permalink

Thanks. I was starting to get angry. :)

nazzilla on August 8 12 / 220 Permalink

i love you :)

Scott on October 25 12 / 298 Permalink

You sir are a saint

noldakong on February 8 13 / 38 Permalink

Oh! no more 404 error! in my blog. Thank you!

Michael on February 13 13 / 43 Permalink

Thank you, thank you, thank you :-)

Mike on February 27 13 / 57 Permalink

Thanks so much for this – saved me a lot of time.

Leave a Comment

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