Why my custom post posts aren’t showing (404 error / page not found)

I just installed a new wp installation. I have created a new child theme based on the TwentyTwelve theme. In the child theme functions.php, I have created a new custom post called “news”. I have added two posts in the “news” and when I view either of them, I get a “page not found” error. See screenshot: http://pasteboard.co/dtIOK6g.jpg

I am expecting the posts to be displayed but am getting a 404 page. why.

Read More

Update (to make my question clear):
From installation, I have done very little work, apart from the following:

  • Added a new child theme (of Twenty Twelve)
  • I have applied the new child theme
  • I have created a new custom post by updating the creating a functions.php in the child theme and ading in this code here (http://pastebin.com/DKny8QH1)
  • In WP admin, I have created a 2 new posts under news (new custom post created above)
  • if I try to access the post, i get 404 (as shown in screenshot above). However, I can access the posts created normally.

Thanks

RESOLVED: This is really weird but I have resolved it. It appears I needed to reselect the “Post name” option for my permalinks settings even though it was already selected. Selecting this option again solved my problem. Sorry about any confusion.

  • I went to settings > permalink

Related posts

Leave a Reply

2 comments

  1. Actually, this is a common problem.

    When you add a custom post type, you’re also registering a new rewrite rule with WordPress. However you must flush your rewrite rules after registering things in order to get WordPress to recognize them.

    With a plugin, this is fixed by hooking your CPT registration to both init (so it’s usable) and the plugin’s activation hook. Then, inside the plugin activation hook, you manually call flush_rewrite_rules() to reset things.

    Since there is no analogous theme activation/deactivation setup, you can use options to set when the theme has been loaded. For example:

    function my_theme_register_cpt() {
        $args = array(
            // ... your initialization stuff
        );
    
        register_post_type( 'news', $news );
    }
    add_action( 'init', 'my_theme_register_cpt' );
    
    function my_theme_deactivate() {
        delete_option( 'my_theme_active' );
    }
    add_action( 'switch_theme', 'my_theme_deactivation' );
    
    $is_active = get_option( 'my_theme_active' );
    if ( 'set' !== $check ) {
        my_theme_register_cpt();
        flush_rewrite_rules();
    
        add_option( 'my_theme_active', 'set', '', 'no' );
    }
    

    In a nutshell. This code will:

    • Register your CPT on init as usual.
    • When you change themes, it clears a stored value stating your theme is active
    • If your theme becomes active and this stored value is not set, the theme will fire your CPT registration code (again) and immediately flush the rewrite rules. Then it sets the stored value so it doesn’t fire flush_rewrite_rules() multiple times.
  2. RESOLVED: This is really weird but I have resolved it. It appears I needed to reselect the “Post name” option for my permalinks settings even though it was already selected. Selecting this option again solved my problem. Sorry about any confusion.

    I went to settings > permalink