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 added the following code to my functions.php file to create a custom post type.

  function create_post_type() {
      register_post_type( 'mysite_reviews',
          array(  
              'labels' => array(  
                  'name' => __( 'Reviews' ),
                  'singular_name' => __( 'Review' )
              ),  
          'public' => true,  
          'menu_position' => 5,  
          'rewrite' => array('slug' => 'reviews')
          )  
      );  
  }  

add_action( 'init', 'create_post_type' );

I then created a copy of my single.php file named single-mysite_reviews.php.

I went to my Wordpress dashboard and the custom post type was showing up in my menu. I created a new post from the new menu and published it. When I tried to view this new post all I could get was an error. Or more specifically, this page: http://gointrigue.com/vanilla/reviews/my-site-reviews-custom-post-type/

What am I doing wrong?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

flush_rewrite_rules() as soon as you register the custom post type. It appears that they have not been rewritten yet, that is, most probably, why you're getting an embarrassing message.

register_post_type( 'mysite_reviews',
        array(  
            'labels' => array(  
                'name' => __( 'Reviews' ),
                'singular_name' => __( 'Review' )
            ),  
        'public' => true,  
        'menu_position' => 5,  
        'rewrite' => array('slug' => 'reviews')
    )
);
flush_rewrite_rules(); // <- do this only once!

Alternatively you can go to Settings -> Permalinks -> Save changes, which calls on flush_rewrite_rules() for you.

In fact http://gointrigue.com/vanilla/?post_type=mysite_reviews shows your post, which means that it's just not being rewritten due to the lack of the proper rules, which have not yet been flushed to the database.

share|improve this answer
    
Yes! Thank you. Resaving my permalink settings did the trick instantly. I should have figured it had something to do with that. Thank you for your help. –  jimmykup Oct 21 '11 at 11:42
    
You are most welcome; glad to have been of help. –  soulseekah Oct 21 '11 at 12:14
add comment

Not really sure what you want to achieve from your description but, if you want a custom post type in the loop, do this:

// Add the 'your_post_type' postype to the loop.
add_action('pre_get_posts', function(WP_Query $query){
    if(is_admin() or is_preview()){
        return;
    }
    // Only add them to the loop on Home/Front-Page
    if((is_home() or is_front_page()) and empty($query->query_vars['suppress_filters']){
        // This has to be an array so fix it if required
        $post_types = $query->get('post_type');
        if(empty($post_types)) $post_types = array('post');
        elseif(is_string($post_types)) $post_types = array($post_types);
        // Add one or more CPT-s to the loop here (merge old with new)
        $query->set('post_type', array_merge($post_types, array(
            'your_post_type',
            // 'another_post_type',
            // 'maybe_another_post_type',
    )));
    }
    return;
});

Custom post types require a bit more manual tuning when you use them. Not only do you need to enable them but also list them or add them to the loop, create an archive (or at least link to it).

PS: Code uses PHP 5.3+ Closures. Convert to PHP 5.2 yourself, as an assignment :)

share|improve this answer
add comment

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.