Bainternet WordPress Consultant
  • WordPress Services
  • Blog
    • Fancy Functions
    • ShortCodes
    • How To’s
  • Plugins
    • Post Editor Zen Coding
    • Advanced Code Editor 2.0
      • other older versions
    • ShortCodes UI
    • User specific Content
    • Simple QR Code
    • Sidebar per user role
    • Simple TOC
    • Post Creation Limits
      • Posts Creation Limits older versions
    • User Ranks
      • Bainternet User Ranks v1.0
    • Custom Category Template
    • Author category
    • Simple Gist Embed
    • WordPress Simple Instant Search Plugin
    • GoTo Redirect
    • Simple Google +1 plugin
  • About Me
    • Donations
  • Contact me

Custom Post type getting 404 on Permalinks

Posted on Feb 03, 2011
Ohad Raz
24 Comments
How To
  • Share on Facebook.
  • Share on Twitter.
  • Share on Google+
  • Share on LinkedIn
Custom Post type getting 404 on Permalinks

Not really an How To but just a quick post to help you guys who are stuck looking for a fix on the 404 problem caused by permalink structure other then the default for custom post type.

Bits of background

So i wrote a short plugin for a client of mine with a custom post type (CPT in short) called deals using what is quickly becoming my favorite WordPress function Register_post_type which accepts a great list of arguments, but in this case I’m talking about the rewrite argument that can accept:

  • False – to prevent rewrite
  • Array -
    • ‘slug’ – prepend posts with this slug – defaults to post type’s name
    • ‘with_front’ – allowing permalinks to be prepended with front base (example: if your permalink structure is /blog/, then your links will be: false->/news/, true->/blog/news/) – defaults to true
    • ‘feeds’ – default to has_archive value
    • ‘pages’ – defaults to true

So as usual i used a simple :

'rewrite' => array('slug' => 'deal') 

and my post type was registered all nice and good and i could see it correct when using the default permalink stracture (?p=32) but when I changed the permalink structure to somthing other then the default i started getting a 404 – page not found and after hours of digging around i found that i need to flush the rewrite rules right after registering the post type.

The Magic Fix

So after finding that out the fix was easy, all i did was add :

flush_rewrite_rules( false );

to my register post type function.

Update a head
please forget everything from above and read the section bellow

Ok So it appears that `flush_rewrite_rules` is a very resource expansive function and therefor running it on every page load is a bad idea so here is a better solution, In your register post type function add the following:

$set = get_option('post_type_rules_flased_POST-TYPE-NAME-HERE');
if ($set !== true){
   flush_rewrite_rules(false);
   update_option('post_type_rules_flased_POST-TYPE-NAME-HERE',true);
}

Hope this helps you guys and saves you some time.

Incoming search terms:

  • custom post type 404
  • permalink 404
  • wordpress custom post type rewrite 404
  • custom post type single 404
  • wordpress custom post types page not found
  • \rewrite\ hierarchical\ => true 404
  • register_post_type 404
  • single post type page not found

Related posts:

  1. how to – WordPress Category Extra Fields






Loading comments...
  • About the author

    Ohad Raz

    I'm a WordPress Consultant, a WordPress Developer and a WordPress Freelancer
    Currently in my third year of Software Engineering B.Sce Degree.
    I've been developing Websites and web applications on WordPress for over 5 years.
    I live in Kiryat Gat , Israel.

    • Write an E-Mail
  • Other Methods of contact

    My profile on WordPress Answers

    profile for Bainternet at WordPress, Q&A for WordPress developers and administrators

    • My Code Poet listing

    • My Careers 2.0 Profile

    • My Gravatar.com Profile

    • My About.Me Profile

    • Contact Form

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.





  • Recent Comments

    • WordPress Developer Ohad Raz on Advanced Code Editor 2.0
    • WordPress Developer anupam on Advanced Code Editor 2.0
    • WordPress Developer Ohad Raz on how to – WordPress Category Extra Fields
    • WordPress Developer Luismin on how to – WordPress Category Extra Fields
    • WordPress Developer Ohad Raz on Advanced Code Editor 2.0
  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

  • Top Posts & Pages

    • WordPress Taxonomies Extra Fields the easy way
    • how to - WordPress Category Extra Fields
    • How I add a WordPress MetaBox
    • Custom taxonomies extra fields


  • About Me
  • Blog
  • Contact me
  • disclaimer
  • Donations
  • home posts
  • WordPress Services
© 2011 by Bainternet WordPress Consultant