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.

No votes yet.
Please wait...
READ  [WordPress] how to Fix 404 Errors for Posts and Pages when moved to Live

We are a WordPress plugin developer company that focuses on useful WordPress plugin creation and empowering people to earn passive incomes from their blogs. We build unique and groundbreaking plugins that will revolutionize your blog!

Click to comment

Leave a Reply

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

More in coding

Sign up for our newsletter and get a free affiliate e-book!


About Me:

Szabi Kisded

Hi, my name is Szabi and I'm documenting my journey selling plugins on CodeCanyon. I will show you every step of it: learning to code, plugin ideas, WordPress stuff and more. Read more…

Mega Plugin Bundle:

CodeCanyon Portfolio:

Learn to Code Plugins:

Latest Promotions:

Recommended Theme:

Start Your Own Blog:

Recommended VPN:

Translate Your Blog:

AdSense Alternative:

Best Article Spinner:

Popular Posts:

Latest Posts:

To Top