Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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
share|improve this question
    
That screenshot is not helpful. The URL that blanked out (in the screenshot), would have been helpful, if it is public. Given the information provided, I see no way to answer your question. Please try to add more detail. –  s_ha_dum Feb 13 '13 at 14:54
    
Sorry but the site is not public yet (if not logged in as admin, you will see a "coming soon" page). –  Abs Feb 13 '13 at 15:20
    
Then your description has to be exceptionally good. Edit your question to include much more detail. –  s_ha_dum Feb 13 '13 at 15:26

2 Answers 2

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.
share|improve this answer
    
This helped me solve the issue. I just changed my permalink settings to default and then back again. Fixed! –  Matthew Blackford Jun 7 '13 at 5:53
up vote 0 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.