admin管理员组文章数量:1130349
I have declared post type as below:
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'agences'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'taxonomies' => array('brands', 'country'),
'supports' => array('title', 'editor', 'author', 'thumbnail', 'custom-fields')
);
register_post_type('destinations', $args);
Initially I was able to access single page of this post type using single-agences.php but now it is redirecting to 404.
I have checked other answers and found that its a common mistake but other answers were not able to resolve this. Any help will be wonderful.
I have declared post type as below:
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'agences'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'taxonomies' => array('brands', 'country'),
'supports' => array('title', 'editor', 'author', 'thumbnail', 'custom-fields')
);
register_post_type('destinations', $args);
Initially I was able to access single page of this post type using single-agences.php but now it is redirecting to 404.
I have checked other answers and found that its a common mistake but other answers were not able to resolve this. Any help will be wonderful.
Share Improve this question edited Mar 7, 2016 at 14:29 jdm2112 3,6851 gold badge17 silver badges23 bronze badges asked Mar 7, 2016 at 14:23 Deepak jhaDeepak jha 4461 gold badge3 silver badges11 bronze badges 3 |2 Answers
Reset to default 12Newly registered CPT shows 404 because, the register_post_type() doesn't flush the rewrite rules. So it's up to you, whether you want to do it manually or automatically.
Manually:
Get to /wp-admin/, then Settings » Permalinks, and then just hit the Save Changes button to flush the rewrite rules.
Automatically:
You can flush the rewrite rules using flush_rewrite_rules() function. But as register_post_type() is called in init hook, it will fire every time when the init hook will fire. The same thing the codex is saying also:
This function is useful when used with custom post types as it allows for automatic flushing of the WordPress rewrite rules (usually needs to be done manually for new custom post types). However, this is an expensive operation so it should only be used when absolutely necessary.
That's why it's better to hook the thing to something that fire once, and that flush rules when necessary only. As @cybmeta already showed that to you. But you can follow @bainternet's approach as well:
/**
* To activate CPT Single page
* @author Bainternet
* @link http://en.bainternet.info/2011/custom-post-type-getting-404-on-permalinks
* ---
*/
$set = get_option( 'post_type_rules_flased_mycpt' );
if ( $set !== true ){
flush_rewrite_rules( false );
update_option( 'post_type_rules_flased_mycpt', true );
}
He is saving a value to options table only for your post type. And if the value is not there, he's flushing rewrite rules. If it's there, he's not doing it.
But please note, it's actually making a db call every time (if not cached). So, I'd prefer hooking the code after_setup_theme for theme, or register_activation_hook for plugin.
Bonus
While debugging rewrite rules, plugin like these could be very helpful:
- Rewrite Rules Inspector by Automattic
- Rewrite Rule Testing by Matthew Boynes
- Debug Bar Rewrite Rules by Frédéric GILLES
It was due to a plugin "Remove taxonomy base slug". It was conflicting with base slug of CPT "destinations". I had to use add_rewrite_rule to check for slug and manually redirect it to proper location
I have declared post type as below:
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'agences'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'taxonomies' => array('brands', 'country'),
'supports' => array('title', 'editor', 'author', 'thumbnail', 'custom-fields')
);
register_post_type('destinations', $args);
Initially I was able to access single page of this post type using single-agences.php but now it is redirecting to 404.
I have checked other answers and found that its a common mistake but other answers were not able to resolve this. Any help will be wonderful.
I have declared post type as below:
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'agences'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'taxonomies' => array('brands', 'country'),
'supports' => array('title', 'editor', 'author', 'thumbnail', 'custom-fields')
);
register_post_type('destinations', $args);
Initially I was able to access single page of this post type using single-agences.php but now it is redirecting to 404.
I have checked other answers and found that its a common mistake but other answers were not able to resolve this. Any help will be wonderful.
Share Improve this question edited Mar 7, 2016 at 14:29 jdm2112 3,6851 gold badge17 silver badges23 bronze badges asked Mar 7, 2016 at 14:23 Deepak jhaDeepak jha 4461 gold badge3 silver badges11 bronze badges 3- 1 Because you are not using the default rewrite rules, you need to flush and rebuild them. See this: custom_post_type with has_archive doesn't change permalink – cybmeta Commented Mar 7, 2016 at 14:42
-
Your single file should be
single-destinations.php, not with the rewrite rule. And your whole code should be inside ainithooked function. – Mayeenul Islam Commented Mar 7, 2016 at 18:47 -
I tried
single-destinations.phpbefore tryingsingle-agences.phpbut that was not working. even if it does not use these file it should usesingle.phpbut its giving a 404 error. and yes code is ininithooked funtion. – Deepak jha Commented Mar 7, 2016 at 18:55
2 Answers
Reset to default 12Newly registered CPT shows 404 because, the register_post_type() doesn't flush the rewrite rules. So it's up to you, whether you want to do it manually or automatically.
Manually:
Get to /wp-admin/, then Settings » Permalinks, and then just hit the Save Changes button to flush the rewrite rules.
Automatically:
You can flush the rewrite rules using flush_rewrite_rules() function. But as register_post_type() is called in init hook, it will fire every time when the init hook will fire. The same thing the codex is saying also:
This function is useful when used with custom post types as it allows for automatic flushing of the WordPress rewrite rules (usually needs to be done manually for new custom post types). However, this is an expensive operation so it should only be used when absolutely necessary.
That's why it's better to hook the thing to something that fire once, and that flush rules when necessary only. As @cybmeta already showed that to you. But you can follow @bainternet's approach as well:
/**
* To activate CPT Single page
* @author Bainternet
* @link http://en.bainternet.info/2011/custom-post-type-getting-404-on-permalinks
* ---
*/
$set = get_option( 'post_type_rules_flased_mycpt' );
if ( $set !== true ){
flush_rewrite_rules( false );
update_option( 'post_type_rules_flased_mycpt', true );
}
He is saving a value to options table only for your post type. And if the value is not there, he's flushing rewrite rules. If it's there, he's not doing it.
But please note, it's actually making a db call every time (if not cached). So, I'd prefer hooking the code after_setup_theme for theme, or register_activation_hook for plugin.
Bonus
While debugging rewrite rules, plugin like these could be very helpful:
- Rewrite Rules Inspector by Automattic
- Rewrite Rule Testing by Matthew Boynes
- Debug Bar Rewrite Rules by Frédéric GILLES
It was due to a plugin "Remove taxonomy base slug". It was conflicting with base slug of CPT "destinations". I had to use add_rewrite_rule to check for slug and manually redirect it to proper location
本文标签: 404 errorSingle custom post type page redirecting to 404 page
版权声明:本文标题:404 error - Single custom post type page redirecting to 404 page 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749143388a2322587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


single-destinations.php, not with the rewrite rule. And your whole code should be inside ainithooked function. – Mayeenul Islam Commented Mar 7, 2016 at 18:47single-destinations.phpbefore tryingsingle-agences.phpbut that was not working. even if it does not use these file it should usesingle.phpbut its giving a 404 error. and yes code is ininithooked funtion. – Deepak jha Commented Mar 7, 2016 at 18:55