I'm trying to achieve the exact same thing shared by Milo in this post : WP Rewrite Rules - Custom post type & taxonomy
Everything works perfectly with pagination but i got a 404 while trying to acess to my single custom post type post.
To explain :
site.com/products = OK
site.com/products/page/2 = OK
site.com/products/category = OK
site.com/products/category/page/2 = OK
site.com/products/category/product-name = 404
Here is my code ($productCategoryBaseSlug is a php variable set somewhere else with a value of "products" in this example) :
/**** CUSTOM POST TYPE ****/
$labels = array(
'name' => __( 'Product', 'Post Type General Name', $this->text_domain ),
'singular_name' => __( 'Product', 'Post Type Singular Name', $this->text_domain ),
'menu_name' => __( 'Products', $this->text_domain ),
'name_admin_bar' => __( 'Product', $this->text_domain ),
'archives' => __( 'Product Archives', $this->text_domain ),
'parent_item_colon' => __( 'Parent Product:', $this->text_domain ),
'all_items' => __( 'All Products', $this->text_domain ),
'add_new_item' => __( 'Add New Product', $this->text_domain ),
'add_new' => __( 'Add New', $this->text_domain ),
'new_item' => __( 'New Product', $this->text_domain ),
'edit_item' => __( 'Edit Product', $this->text_domain ),
'update_item' => __( 'Update Product', $this->text_domain ),
'view_item' => __( 'View Product', $this->text_domain ),
'search_items' => __( 'Search Product', $this->text_domain ),
'not_found' => __( 'Not found', $this->text_domain ),
'not_found_in_trash' => __( 'Not found in Trash', $this->text_domain ),
'featured_image' => __( 'Featured Image', $this->text_domain ),
'set_featured_image' => __( 'Set featured image', $this->text_domain ),
'remove_featured_image' => __( 'Remove featured image', $this->text_domain ),
'use_featured_image' => __( 'Use as featured image', $this->text_domain ),
'insert_into_item' => __( 'Insert into Product', $this->text_domain ),
'uploaded_to_this_item' => __( 'Uploaded to this Product', $this->text_domain ),
'items_list' => __( 'Products list', $this->text_domain ),
'items_list_navigation' => __( 'Products list navigation', $this->text_domain ),
'filter_items_list' => __( 'Filter Products list', $this->text_domain ),
);
$args = array(
'label' => __( 'Product', $this->text_domain ),
'description' => __( 'Replacement for text post', $this->text_domain ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
'taxonomies' => array( 'product_category', 'brand'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => $productCategoryBaseSlug,
'exclude_from_search' => false,
'publicly_queryable' => true,
'query_var' => false,
'capability_type' => 'post',
'rewrite' => array(
'slug' => $productCategoryBaseSlug.'%product_category%',
'with_front' => false
)
);
register_post_type( 'product', $args );
/**** CUSTOM TAXONOMY ****/
$genre_args = array(
'hierarchical' => true,
'labels' => array(
'name'=> __('Categories', 'taxonomy general name', $this->text_domain),
'singular_name' => __('Category', 'taxonomy singular name', $this->text_domain),
'search_items' => __('Search Categories', $this->text_domain),
'popular_items' => __('Popular Categories', $this->text_domain),
'all_items' => __('All Categories', $this->text_domain),
'edit_item' => __('Edit Category', $this->text_domain),
'update_item' => __('Update Category', $this->text_domain),
'add_new_item' => __('Add New Category', $this->text_domain),
'new_item_name' => __('New Category Name', $this->text_domain),
'separate_items_with_commas' => __('Seperate Categories with Commas', $this->text_domain),
'add_or_remove_items' => __('Add or Remove Categories', $this->text_domain),
'choose_from_most_used' => __('Choose from Most Used Categories', $this->text_domain)
),
'public' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => array(
'with_front' => false,
'slug' => $productCategoryBaseSlug /*__( 'product_category', 'product-category', $this->text_domain )*/
)
);
register_taxonomy('product_category', 'product', $genre_args);
/**** REWRITE ****/
add_rewrite_rule(
$productCategoryBaseSlug.'/([^/]+)/page/([0-9]+)/?$',
'index.php?product_category=$matches[1]&paged=$matches[2]',
'top'
);
/**** CONTENT OF PERMALINK FUNCTION ****/
if ( is_object( $post ) && $post->post_type == 'product' ){
$terms = wp_get_object_terms( $post->ID, 'product_category' );
if( $terms ){
//return str_replace( '%product_category%' , $terms[0]->slug , $post_link );
$post_link = str_replace('%product_category%', get_taxonomy_parents(array_pop($terms)->term_id, 'product_category', false, '/', true), $post_link);
}
}
Can someone help me about that ? I'm stuck for hours and can't find a solution :/
EDIT : As explained in comment below, i made a mistake in the custom post type declaration, a slash was missing, new code is :
'rewrite' => array(
'slug' => $productCategoryBaseSlug.'/%product_category%',
'with_front' => false
)
BUT NOW, th result is the following :
site.com/products = OK
site.com/products/page/2 = OK
site.com/products/category = OK
site.com/products/category/page/2 = BROKEN, Leads to site.com/page/2
site.com/products/category/product-name = OK
I'd live to read Milo's advice (https://wordpress.stackexchange.com/users/4771/milo) about this as he seems to be the real expert of rewrite rules !
Thank you