admin管理员组

文章数量:1426943

Hitting a brick wall with the following:

I have:

  • 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' );

Hitting a brick wall with the following:

I have:

  • 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' );
Share Improve this question edited Jul 17, 2011 at 15:35 anu asked Jul 17, 2011 at 14:39 anuanu 9,5828 gold badges46 silver badges64 bronze badges 4
  • 1 you've flushed rewrites first by visiting permalinks page and saving? – Milo Commented Jul 17, 2011 at 15:26
  • @milo - yup. Interestingly, after walking away for an hour and then coming back, the 'pretty permalinks' are now working - I'll update the question and frame it as a 'why the delay' – anu Commented Jul 17, 2011 at 15:33
  • Do you use a caching plugin? Does it work faster with all other plugins disabled? (Just getting the usual suspects out of the way) – Jan Fabry Commented Jul 18, 2011 at 21:21
  • @jan - no caching plugin. The problem is that I can't reproduce this behaviour - it's happened a couple of times now and goes away after but with no specific action (that I can tell) to actually fix it. – anu Commented Jul 19, 2011 at 13:42
Add a comment  | 

2 Answers 2

Reset to default 8

Use the function flush_rewrite_rules() for set the rewrite rules new, but not with your code on init-hook, only on activation plugin or theme! See more in my post: http://wpengineer/2044/custom-post-type-and-permalink/

global $wp_rewrite;
$wp_rewrite->flush_rules();

Flush rules only on activation (and deactivation). Don't do it on any other hook.

register_activation_hook()

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

本文标签: Custom Post Type PermalinkRewrite not working immediately