Why do my custom post types show up in the dashboard, but not on my site?

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.

Read More

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?

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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 🙂