Opened 13 years ago
Closed 11 years ago
#21417 closed defect (bug) (invalid)
Custom Post Type Permalink Returns 404 Error
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | minor | Version: | 3.4.1 |
| Component: | Permalinks | Keywords: | reporter-feedback |
| Focuses: | Cc: |
Description
Since, I upgraded Wordpress version from 3.3.2 to 3.4.1, I discovered permalink returns 404 error on (1) Paged categories, and (2) Custom type posts.
The paged categories has been fixed implementing ticket:21209, but custom type posts return 404 error when it called.
I am using following code to register custom type post 'products'.
function products_register() {
$labels = array(
'name' => _x('Products', 'post type general name'),
'singular_name' => _x('Product', 'post type singular name'),
'add_new' => _x('Add New', 'product item'),
'add_new_item' => __('Add New Product'),
'edit_item' => __('Edit Product'),
'new_item' => __('New Product'),
'view_item' => __('View Product'),
'search_items' => __('Search Product'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
// working perfect in 3.3.2 'rewrite' => true,
// also tried this in 3.4.1 'rewrite' => array('slug' => 'products','with_front' => FALSE),
'rewrite' => array('slug' => 'products'),
'capability_type' => 'post',
'hierarchical' => false,
'can_export' => true,
'menu_position' => 5,
'taxonomies' => array('category'),
'supports' => array('title','editor','thumbnail', 'custom-fields')
);
register_post_type( 'products' , $args );
}
//Registering Post-Type Products for Affiliate products only
add_action('init', 'products_register');
function add_default_boxes() {
register_taxonomy_for_object_type('category', 'products');
}
//Registering Taxonomy for Products post-type
add_action('init', 'add_default_boxes');
Change History (7)
#2
follow-up:
↓ 4
@
13 years ago
After upgrading Wordpress to 3.4.1, I faced 404 error with 'paged' categories, but it was soon resolved with assistance available in ticket:21209. I added following code to the theme's functions.php file.
function hc_init() {
global $wp_rewrite;
//add rewrite rule.
add_rewrite_rule("author/([^/]+)/page/?([0-9]{1,})/?$",'index.php?author_name=$matches[1]&paged=$matches[2]','top');
add_rewrite_rule("(.+?)/page/?([0-9]{1,})/?$",'index.php?category_name=$matches[1]&paged=$matches[2]','top');
$wp_rewrite->flush_rules(false);
}
add_action('init','hc_init');
As I explained earlier that my above code was super fit, and had no problem related to permalink and/or custom post type before upgrade to 3.4.1.
My steps:
- Yes, I added code mentioned-above to the theme's functions.php file.
- Yes, flushed rewrite rules by visiting the Permalink Settings screen.
- Yes, I created a new product (Product 1).
- But, when I visited /products/product-1/. The post returns 404 error as tested it.
Please help me to fix 404 error with custom post type.
#4
in reply to:
↑ 2
@
13 years ago
Replying to imageac:
$wp_rewrite->flush_rules(false);
Note that $wp_rewrite->flush_rules() call is only needed once after a new custom post type is added (as described in the Codex articles on flush_rules and WP_Rewrite), not on each page load.
Please help me to fix 404 error with custom post type.
Have you tried disabling all plugins and switching to a default theme (Twenty Eleven on 3.4.1), then flushing rewrite rules once again?
Until the exact bug is identified and confirmed, I'd suggest trying the support forums to get help: http://wordpress.org/support/
#5
@
13 years ago
- Keywords needs-patch needs-codex removed
- Severity changed from critical to minor
You hook seems incorrect to me - when adding custom rewrite rules, it is safer to use this action or filter:
do_action_ref_array('generate_rewrite_rules', array(&$this));
$this->rules = apply_filters('rewrite_rules_array', $this->rules);
Here's a great example:
http://bbpress.trac.wordpress.org/browser/branches/2.1/bbpress.php#L830
I couldn't reproduce the 404 error using your example on a clean install with Twenty Twelve.
My steps:
functions.phpfile.