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 have created custom post type 'event' to display in single page named single-event.php file but it provides me 404 error. how i can solve this problem even i used

flush_rewrite_rules();

please provide some idea.......

function custom_event() {
$labels = array( 
   'name' => _x('Event', 'post type general name'),
   'singular_name' => _x('Event', 'post type singular name'),
   'add_new' => _x('Add Event', 'Content'),
   'add_new_item' => __('Add New Event'),
   'edit_item' => __('Edit Event'),
   'new_item' => __('New Content'),
   'view_item' => __('View Event'),
   'search_items' => __('Search Content'),
   'not_found' =>  __('Nothing found'), 
   'parent_item_colon' => '',
   'menu_name' => __('Event')
);

 $args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true, 
'show_in_menu' => true, 
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true, 
'hierarchical' => false,
'menu_position' => 20,
'supports' => array( 'title', 'editor')
 );

flush_rewrite_rules();
register_post_type('event', $args);
}
  //$wp_rewrite->flush_rules();
  //add_action('admin_init', 'flush_rewrite_rules');
    add_action ('init', 'custom_event');
share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

Follow this tutorial for creating the custom post type.

Here is the working code as per your requirement:

function create_post_type_event() {

    //Create custom post type for event

    $labels = array(
            'name' => _x('Event', 'post type general name'),
            'singular_name' => _x('Event', 'post type singular name'),
            'add_new' => _x('Add New', 'event'),
            'add_new_item' => __('Add New Event'),
            'edit_item' => __('Edit Event'),
            'new_item' => __('New Event'),
            'all_items' => __('All Events'),
            'view_item' => __('View Event'),
            'search_items' => __('Search Event'),
            'not_found' =>  __('No event found'),
            'not_found_in_trash' => __('No event found in Trash'),
            'parent_item_colon' => '',
            'menu_name' => __('Event')

    );
    $args = array(
            'labels' => $labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'query_var' => true,
            'rewrite' => TRUE,
            'capability_type' => 'post',
            'has_archive' => true,
            'hierarchical' => false,
            'menu_position' => 5,
            'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields' )
    );
    register_post_type('event',$args);

    flush_rewrite_rules();          // Flush the rewrite rules and re-create the above for custom post types

}

add_action( 'init', 'create_post_type_event' );

After creating this cpt you can create a single-event.php file in your active theme's directory and it should work with no trouble.

share|improve this answer
add comment

Place flush_rewrite_rules(); after the register call. Also, Try flushing your rewrite rules through WP Admin, by visiting WP Admin -> Settings -> Permalink -> Press Save button.

share|improve this answer
add comment

Try using 'rewrite' => array( 'slug' => 'event' ) instead of 'rewrite' => true.

Also, you shouldn't use flush_rewrite_rules() like this. It will run unnecessarily each time. You could make a plugin to define the custom post type and flush the rewrite rules using register_activation_hook.

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.