either single-{custom}.php 404's OR /taxonomy/ 404's - custom post type and taxonomy permalinks

I've been playing around with custom post types and taxonomies and banging my head off walls on numerous occasions now - I believe I've read every forum on the internet on how to solve various issues and I'm close with no cigar.

I've reset permalinks, added flush_rewrite_rules(); and all the rest

I can get the taxonomy to work e.g. domainname.com/products/audio-products BUT then the single-{custom}.php 404's

A slug of 'products' , '%product_cat%' or the one used in the code below will work:

register_taxonomy('product_cat',array('product'), array(  
    'hierarchical' => true,  
    'labels' => $labels,
'rewrite' => array( 'slug' => 'products/%product_cat%','with_front' =>FALSE, 'hierarchical' => true ),
    'query_var' => true,  
    'show_ui' => true  
 ));  

 add_action( 'init', 'build_taxonomies', 0 );  

OR

I can get the single-{custom}.php to display but then domainname.com/products/audio-products will 404!!

register_taxonomy('product_cat',array('product'), array(  
    'hierarchical' => true,  
    'labels' => $labels,
    'rewrite' => array( 'slug' => 'pcat','with_front' => FALSE, 'hierarchical' => true ),
    'query_var' => true,  
    'show_ui' => true  
 ));  

 add_action( 'init', 'build_taxonomies', 0 );  

Here is my full code:

$labels = array(  
    'name' => _x( 'Product Categories', 'taxonomy general name' ),  
    'singular_name' => _x( 'Product Category', 'taxonomy singular name' ),  
    'search_items' =>  __( 'Search Product Categories' ),  
    'all_items' => __( 'All Product Categories' ),  
    'parent_item' => __( 'Parent Category' ),  
    'parent_item_colon' => __( 'Parent Category:' ),  
    'edit_item' => __( 'Edit Product Category' ),  
    'update_item' => __( 'Update Product Category' ),  
    'add_new_item' => __( 'Add Product Category' ),  
    'new_item_name' => __( 'New Product Category' ),  
    'menu_name' => __( 'Product Categories' )  
  );      

register_taxonomy('product_cat',array('product'), array(  
    'hierarchical' => true,  
    'labels' => $labels,
    'rewrite' => array( 'slug' => 'products/%product_cat%','with_front' => FALSE, 'hierarchical' => true ),
    'query_var' => true,  
    'show_ui' => true  
 ));  

 add_action( 'init', 'build_taxonomies', 0 );  


function build_taxonomies() {  
register_taxonomy( 
'brands', 
array( 'product','post' ), 
array( 
'hierarchical' => true, 
'labels' => array(
            'name' => 'Brands',
            'singular_name' => 'Brand',
            'update_item' => 'Edit Brand',
            'add_new_item' => 'New Brand',
            'view_item' => 'View Brand',
            'search_items' => 'Search Brand'
        ), 
'query_var' => true, 
'rewrite' => array( 'slug' => 'brands','with_front' => FALSE, 'hierarchical' => true ), ) );  
}  


add_action('init', 'create_post_type');

function create_post_type() {
    register_post_type('product', array(
        'labels' => array(
            'name' => 'Products',
            'singular_name' => 'Product',
            'add_new' => 'Add new product',
            'edit_item' => 'Edit product',
            'new_item' => 'New product',
            'view_item' => 'View product',
            'search_items' => 'Search products',
            'not_found' => 'No products found',
            'not_found_in_trash' => 'No products found in Trash'
        ),

        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'has_archive' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'products','with_front' => FALSE),
        'capability_type' => 'post',
        'hierarchical' => false,
        'supports' => array(
            'title',
            'editor',
            'custom-fields',
            'thumbnail',
            'excerpt'
        ),
        'menu_position' => 5,
        'taxonomies' => array('post_tag', 'product_cat' , 'brands') // this is IMPORTANT
    ));
    flush_rewrite_rules();
}


add_filter( 'pre_get_posts', 'my_get_posts' );


function my_get_posts( $query ) {

if ( is_home() && $query->is_main_query() )
    $query->set( 'post_type', array( 'post', 'page', 'product' ) );

return $query;
}

Solutions

Well a few weeks on and my answer does seem to work - its interesting hearing people talk of using filters - as far as I am aware they are not necessary.

Whether /%product_cat% has any effect I am not sure so for my solution I'm leaving it out:

Product_Cat taxonomy should be:

'rewrite' => array( 'slug' => 'products','with_front' =>FALSE, 'hierarchical' => true )

Products Custom Post type needs to change to: 'rewrite' => array( 'slug' => 'product','with_front' => FALSE ),

Similar questions

PHP while loop ODD and Even wrapped in either the left or right column
I cant seem to get this right, basically I have a while loop and i want all odd posts to get wrapped in a div called .split-left, so 1,3,5 etc will go into that column. Then all EVEN posts must go into .split-right div so 2, 4, 6 etc. Currently my loop is putting post 1 into the .split-left div but then its putting 2, 4, 6 into the split-left div a...
Custom Single Post Type not referring to single-post-type.php File
I've defined a Custom Post type called "Press", I'm trying to modify the Single Posts template for that Custom Type. The single posts custom file is called "single-press.php" I've tried using an if in_category(cat_number), get_template_part type statement in single.php & it seems to give me a White Screen rather than the page ... This is the co...
Custom Post Type single post type shown in theme single.php
I am creating a plugin where i have created a custom post type smart-event and I want to serve the single posts under this category via a single page.That's why I have created a single post page named : single-event.php directly into plugin folder -like- event > single-event.php But the single post is being shown in theme single.php page not in ...
Permalinks: custom post type -> custom taxonomy -> custom sub taxonomy -> post
I am building a custom post type and its respective custom taxonomy. Here is the code: I am the doing a rewrite to include this in one pretty URL: I want to include any sub category in the URL. So for example, I have the following structure: The URL structure should be the following www.domain.com/custom_post_type/main-category/sub-category/sample-...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .