Connect with us

[WordPress] Custom Post Type Single Page not Found 404 Error

coding

[WordPress] Custom Post Type Single Page not Found 404 Error

This is a quick post on very frustrating problem I faced today. I have created custom post type and taxonomy. I used permalinks /%postname%/ and used rewrite to true in registering custom post type. When I try to open the front page for that, it ends up with 404 Error page not found.

I thought it may be .htaccess issue but its not. It may be Permalinks issues, visited permalinks page under Dashboard > Settings > Permalinks. None of them works. Generally WordPress handles the Permalinks .htaccess rules if you set .htaccess file to 0666 permission. But for custom post type, it’s not yet. so you need to flush the rules with WordPress functions. All you need to do is call flush_rewrite_rules(); function just below register_post_type(…) function.

Below is the working code for custom post type.

add_action( 'init', 'crest_event_custom_init' );
function crest_event_custom_init() {
  $labels = array(
    'name' => _x('Event', 'post type general name'),
    'singular_name' => _x('Event', 'post type singular name'),
    'add_new' => _x('Add New Event', 'event'),
    'add_new_item' => __('Add New Event'),
    'edit_item' => __('Edit Event'),
    'new_item' => __('New Event'),
    'all_items' => __('All Event'),
    'view_item' => __('View Event'),
    'search_items' => __('Search Event'),
    'not_found' =>  __('No Event Data found'),
    'not_found_in_trash' => __('No Event Data 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' => array('slug' => 'event','with_front' => FALSE),
    'capability_type' => 'post',
    'has_archive' => false, 
    'hierarchical' => false,
    'menu_position' => 9,
    'supports' => array( 'title', 'editor', 'thumbnail','excerpt','custom-fields','comments')
  ); 

  register_post_type('event',$args);
  flush_rewrite_rules();
}

Note : Make sure you write the function just next to register_post_type(…) and not at the bottom of functions.php file or anywhere else. It won’t work if you change the position to somewhere else.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More in coding

To Top
Grab the largest plugin bundle from CodeCanyon!