Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
    
You can build up your permalink sturcture to use hierachical taxonomies in link: wordpress.stackexchange.com/questions/55068/… –  Diana Jun 15 '12 at 15:37
add comment

1 Answer

up vote 1 down vote accepted

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 ),

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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