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
|
Show 1 more comment
2 Answers
Reset to default 4Don'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
flush_
notflash_
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