Fix Custom Post Type Single Pages Returning 404 in WordPress

Quick Summary
- Open Settings > Permalinks to fix 404s on custom post type single pages: the screen rebuilds the rewrite rules WordPress stores in the database.
- If the 404 stays, check that the post type is publicly queryable and that no taxonomy or other post type registered earlier uses the same slug.
- A missing single-{post_type}.php template never causes a 404, because WordPress falls back to single.php, singular.php and index.php.
When the single pages of a WordPress custom post type return a 404 error, the rewrite rules are usually out of date. Open Settings > Permalinks in the dashboard, which rebuilds them, and reload the page. If it still returns 404, check how the post type is registered and whether another post type, taxonomy or archive uses the same slug.
Why do custom post type pages return 404?
WordPress turns a pretty URL such as /book/my-book/ into a database query with a list of rewrite rules. It stores that list in the database and only rebuilds it when something flushes the rules. A post type that was registered after the last flush, or whose slug changed since, has no rule for its URLs: the request falls through to another rule, for example the one for attachments, finds nothing and ends in “Page not found”. As soon as WordPress rebuilds the rules, the same URL loads the post.
Solution 1: re-save your permalinks
Go to Settings > Permalinks in the dashboard. The screen calls flush_rewrite_rules() every time it loads, so opening it is enough; clicking Save Changes does the same and changes nothing else. It’s the quickest fix, and worth repeating whenever you add a post type or change a post type’s URL slug.
Solution 2: flush the rewrite rules in code
If you can’t open the dashboard, for example on a site you deploy to, add this line to your theme’s functions.php file:
flush_rewrite_rules( false );flush_rewrite_rules() clears the stored rules and generates them again, including the rules for your custom post types. Called this early, it waits until the wp_loaded hook, so every post type is registered first. Passing false makes it a soft flush: only the rules in the database are updated, and the .htaccess file is left alone.
Load any page of the site once, check that the single pages work, and then remove the line. Flushing is slow, and as long as the line is there it runs on every request. In a plugin, flush once in the activation hook, after registering the post type. In a theme you deploy to several environments, a version number that triggers one flush when a slug changes, as in how I structure a WordPress project, does the same job without a line to remember to delete.
Still 404 after flushing? Check these
The post type isn’t publicly queryable
publicly_queryable defaults to the value of public. A post type registered with 'public' => false therefore has no front-end single pages: WordPress drops the post type’s query variable from the request, so the URL shows your homepage instead of the post. If you want the post type hidden from search and menus but still viewable, set 'publicly_queryable' => true, as in this register_post_type() example, and flush again.
A taxonomy or another post type uses the same slug
WordPress checks the rewrite rules in order and uses the first match. If a taxonomy and a post type share the slug portfolio, the one registered first owns URLs like /portfolio/something/. With the taxonomy first, /portfolio/my-project/ is read as a term, no term has that name, and the project returns 404; with the post type first, the term pages return 404 instead. Give each its own base, for example portfolio for the post type and portfolio/category for the taxonomy, and flush.
A page has the same slug as the post type
A page doesn’t break the single pages: the post type’s rules come before the page rules, so /about/my-project/ still loads the project. But if the post type has an archive at the same address, the archive takes over and the page at /about/ can no longer be reached. Rename one of the two.
Only the archive page returns 404
If /book/my-book/ works but /book/ doesn’t, the post type has no archive. has_archive defaults to false; register the post type with 'has_archive' => true, or a string to use as the archive slug, and flush.
A cached 404
If a caching plugin or your host’s page cache stored the error page, clear the cache after flushing and test again.
Can a missing template cause the 404?
No. The template only decides how a post that was found is displayed. For a single post of the type book, WordPress looks for these files in the theme, in this order:
- a template chosen for that entry in the editor, if the theme offers one for the post type
single-book-{post-slug}.phpsingle-book.phpsingle.php, thensingular.php, thenindex.php
Block themes follow the same order with HTML templates, such as templates/single-book.html. So if your theme’s 404 template appears, the query found nothing, and the fixes above apply. If the page loads but looks wrong or shows no content, the template is the place to look; an empty single-{post_type}.php template covers that case.
Related articles
- an empty single-{post_type}.php template: the next problem when the page loads but shows no content
- page/2/ returning 404 on archive pages: a different 404, fixed with pre_get_posts
- changing a custom post type’s slug: the filter, the flush and redirects from the old URLs

