1

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

  • Further note : if i change the rewrite slug in the custom post type declaration to : 'slug' => $productCategoryBaseSlug.'/%product_category%' (added a slash before %product_category%) the single page is working fine but pagination is broken for categories :/ – Amandine Sep 14 '17 at 8:01
0

Don't really know why but here is a working solution :

        $blogCategoryBaseSlug = 'articles';
        $labels = array(
            'name'                  => __( 'Blog Post', 'Post Type General Name', $this->text_domain ),
            'singular_name'         => __( 'Blog Post', 'Post Type Singular Name', $this->text_domain ),
            'menu_name'             => __( 'Blog Posts', $this->text_domain ),
            'name_admin_bar'        => __( 'Blog Post', $this->text_domain ),
            'archives'              => __( 'Blog Post Archives', $this->text_domain ),
            'parent_item_colon'     => __( 'Parent Blog Post:', $this->text_domain ),
            'all_items'             => __( 'All Blog Posts', $this->text_domain ),
            'add_new_item'          => __( 'Add Blog Post', $this->text_domain ),
            'add_new'               => __( 'Add New', $this->text_domain ),
            'new_item'              => __( 'New Blog Post', $this->text_domain ),
            'edit_item'             => __( 'Edit Blog Post', $this->text_domain ),
            'update_item'           => __( 'Update Blog Post', $this->text_domain ),
            'view_item'             => __( 'View Blog Post', $this->text_domain ),
            'search_items'          => __( 'Search Blog Post', $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 Blog Post', $this->text_domain ),
            'uploaded_to_this_item' => __( 'Uploaded to this Blog Post', $this->text_domain ),
            'items_list'            => __( 'Blog Posts list', $this->text_domain ),
            'items_list_navigation' => __( 'Blog Posts list navigation', $this->text_domain ),
            'filter_items_list'     => __( 'Filter Blog Posts list', $this->text_domain ),
        );
        $args = array(
            'label'                 => __( 'Blog Post', $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( 'blog_post_category', 'blog_post_tag'),
            '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'           => $blogCategoryBaseSlug,   
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'capability_type'       => 'post',
            'rewrite' => array(
                'slug' => $blogCategoryBaseSlug.'/%blog_post_category%',
                'with_front' => false
            )        
        );
        register_post_type( 'blogpost', $args );


        $genre_args = array( 
            '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,
            'hierarchical' => true,
            'query_var' => true,
            'rewrite' => array(
                'with_front' => false,
                'hierarchical' => true,
                'slug' => $blogCategoryBaseSlug
            )        
        );
        register_taxonomy('blog_post_category', 'blogpost', $genre_args);

        add_rewrite_rule(
            $blogCategoryBaseSlug.'/([^/]+)/page/([0-9]+)/?$',
            'index.php?blog_post_category=$matches[1]&paged=$matches[2]',
            'top'
        );

        add_rewrite_rule(
            $blogCategoryBaseSlug.'/(.+?)/([^/]+)(?:/([0-9]+))?/?$',
            'index.php?blog_post_category=$matches[1]&blogpost=$matches[2]&paged=$matches[3]',
            'top'
        );

But there is still an issue concerning the rewrite rules ... So far, about everything is working :

site.com/articles= OK

site.com/articles/page/2 = OK

site.com/articles/category = OK

site.com/articles/category/page/2 = OK

site.com/articles/category/article-name = OK

But if i create a subcategory, it doesn'work :

site.com/articles/category/subcategory = 404 (looking for a post and not a sub category)

Is there any solution in the rewrite rules to fix that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.