custom post types - flush_rewrite_rules on every register_post_type?

I'm stuck in an awful situation and I think it's because I've registered 5 post types but calling flash_r

I'm stuck in an awful situation and I think it's because I've registered 5 post types but calling flash_rewrite_rules() function just once, so I have this question:

Should I call flash_rewrite_rules() function after each registration of custom post type or just one call?

And by the way, is it OK to have 10 custom post type for a theme (or as many as we want)? any effect on performance?

Thank you.

    <?php 

/*** Registering book post type ***/
function px_book() {
  $labels = array(
    'name'               => 'books',
    'singular_name'      => 'book',
    'menu_name'          => 'books',
  );

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

  register_post_type( 'book', $args );
}
add_action( 'init', 'px_book' );

/*** Registering book post type ***/
function px_movie() {
  $labels = array(
    'name'               => 'movies',
    'singular_name'      => 'movie',
    'menu_name'          => 'movies',
  );

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

  register_post_type( 'movie', $args );
}
add_action( 'init', 'px_movie' );

add_action( 'after_switch_theme', 'flush_rewrite_rules' );
?>

or like this:

    <?php 

/*** Registering book post type ***/
function px_book() {
  $labels = array(
    'name'               => 'books',
    'singular_name'      => 'book',
    'menu_name'          => 'books',
  );

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

  register_post_type( 'book', $args );
}
add_action( 'init', 'px_book' );
add_action( 'after_switch_theme', 'flush_rewrite_rules' );

/*** Registering book post type ***/
function px_movie() {
  $labels = array(
    'name'               => 'movies',
    'singular_name'      => 'movie',
    'menu_name'          => 'movies',
  );

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

  register_post_type( 'movie', $args );
}
add_action( 'init', 'px_movie' );

add_action( 'after_switch_theme', 'flush_rewrite_rules' );
?>

I'm stuck in an awful situation and I think it's because I've registered 5 post types but calling flash_rewrite_rules() function just once, so I have this question:

Should I call flash_rewrite_rules() function after each registration of custom post type or just one call?

And by the way, is it OK to have 10 custom post type for a theme (or as many as we want)? any effect on performance?

Thank you.

    <?php 

/*** Registering book post type ***/
function px_book() {
  $labels = array(
    'name'               => 'books',
    'singular_name'      => 'book',
    'menu_name'          => 'books',
  );

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

  register_post_type( 'book', $args );
}
add_action( 'init', 'px_book' );

/*** Registering book post type ***/
function px_movie() {
  $labels = array(
    'name'               => 'movies',
    'singular_name'      => 'movie',
    'menu_name'          => 'movies',
  );

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

  register_post_type( 'movie', $args );
}
add_action( 'init', 'px_movie' );

add_action( 'after_switch_theme', 'flush_rewrite_rules' );
?>

or like this:

    <?php 

/*** Registering book post type ***/
function px_book() {
  $labels = array(
    'name'               => 'books',
    'singular_name'      => 'book',
    'menu_name'          => 'books',
  );

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

  register_post_type( 'book', $args );
}
add_action( 'init', 'px_book' );
add_action( 'after_switch_theme', 'flush_rewrite_rules' );

/*** Registering book post type ***/
function px_movie() {
  $labels = array(
    'name'               => 'movies',
    'singular_name'      => 'movie',
    'menu_name'          => 'movies',
  );

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

  register_post_type( 'movie', $args );
}
add_action( 'init', 'px_movie' );

add_action( 'after_switch_theme', 'flush_rewrite_rules' );
?>
Share Improve this question edited Jun 6, 2015 at 6:03 pershianix asked Jun 4, 2015 at 22:19 pershianixpershianix 614 silver badges10 bronze badges 6
  • What is the "awful situation"? What is the problem? – s_ha_dum Commented Jun 4, 2015 at 22:20
  • Wordpress acts weird when using flash_rewrite_rules() function. Sometimes it works and sometimes it doesn't! I think it's because I'm calling flash_rewrite_rules() just once. how many time do I have to call this function? On every creation of new custom post type or just once? Thank you. – pershianix Commented Jun 5, 2015 at 6:36
  • Sorry, "acts weird" is not actionable debugging information. And there is no code to analyze so it is hard to say what is happening, other that it should be flush_ not flash_ and you should only run the function when the CPT is created and not on every page load. – s_ha_dum Commented Jun 5, 2015 at 14:16
  • Some info maybe enlighten the question/problem (flush)Codex. And when it is about amount of custom post types... if done well (coded) and used well it is up to you (imho), 10/20 50 name it. But don't overdo, and you maybe look at(change) your goal/strategie. (also just mho) – Charles Commented Jun 6, 2015 at 3:05
  • Sorry, my post is now more complete. So which is better? Thank you. – pershianix Commented Jun 6, 2015 at 6:06
 |  Show 1 more comment

2 Answers 2

Reset to default 4

Don't call flush_rewrite_rules. It's a super expensive call to make and slows everything down.

Also the order matters, but the correct thing to do is register all your content types, then re-save permalinks in WP Admin, and then... nothing, you've done everything. Only flush permalinks when things have changed, not on every page load. Visiting the permalinks page should be enough to flush them.

Also, don't register post types and taxonomies inside themes, you'll loose access to the data when the user switches to another theme. Themes are for looks, plugins are for functionality

Your problem is that you are flushing the rewrite rules before the post types are registered. Your after_switch_theme should look something like this:

function px_book() {
  $labels = array(
    'name'               => 'books',
    'singular_name'      => 'book',
    'menu_name'          => 'books',
  );

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

  register_post_type( 'book', $args );
}
add_action( 'init', 'px_book' );

function px_movie() {
  $labels = array(
    'name'               => 'movies',
    'singular_name'      => 'movie',
    'menu_name'          => 'movies',
  );

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

  register_post_type( 'movie', $args );
}
add_action( 'init', 'px_movie' );

add_action( 'after_switch_theme', function() {

    // At this point, post types are not registered

    // register post types
    px_movie();
    px_book();

    // flush rewrite rules
    flush_rewrite_rules();

} );

Why? When you click on "Activate" a theme, the init event in the next page load is triggered before your theme is activated, so on after_switch_theme the functions you hook on init are not triggered. It is similar what is described in register_post_type on Codex when registering post types in plugins (by the way, I think registering post types should be done in plugins, not in themes. Themes are for look and feel, post types are content structure, not look and feel. If you switch to another theme you lose the content, that is very bad).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742341619a4425741.html

相关推荐

  • custom post types - flush_rewrite_rules on every register_post_type?

    I'm stuck in an awful situation and I think it's because I've registered 5 post types but calling flash_r

    6小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信