Custom Post Type Permalink / Rewrite not working immediately

Hitting a brick wall with the following:

I have:

Read More
  • 1 custom post type called cpt_community
  • 1 custom taxonomy called tax_community

If I set 'rewrite' => true in my CPT registration, then permalinks to an entry for this CPT are of the form of http://<domain>/cpt_community/test_item/, and I get a 404 when browsing to it.

If I set 'rewrite' => false, then permalinks are http://<domain>/?cpt_community=test_item/, and this works fine.

So, I’m obviously doing something wrong/stupid – the question is, what?

[Update]

  • After every change I’m flushing rules by going to Settings > Permalinks (and saving)
  • After leaving everything alone for an hour, things have started working correctly – so why the delay?

Code

CPT Registration

function community_post_type() {
  $labels = array('name'  => 'Community');

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

  register_post_type('cpt_community', $args);
}  
add_action( 'init', 'community_post_type' );

Custom Taxonomy Registration

function community_tax_type() {
  register_taxonomy(
    'tax_community',
    'cpt_community',
     array( 'hierarchical' => false,
       'label' => 'Community Content Type',
       'show_ui' => true,'query_var' => true,
       'rewrite' => true,
       'singular_label' => 'Community Content Type',
       'capabilities' => array('assign_terms' => 'edit_community_tags')
       )
   );
   # allow roles to add community taxonomy tags to a community CPT
   $roles = array("subscriber","contributor","author","editor","administrator");

   foreach ($roles as $role_name) {
     $role = get_role($role_name);
     $role->add_cap("edit_community_tags");
   }   
}
add_action( 'init', 'community_tax_type' );

Related posts

Leave a Reply

2 comments

  1. Just go to Settings>Permalinks to flash the rules. No code is needed.
    You don’t need to update the structure, just opening that admin page does the job