I want that my WordPress follow this rules:

  1. /productes/ a page with all products
  2. /productes/[category]/ a taxonomy-page with all products of the category
  3. /productes/[category]/[subcategory]/ a taxonomy-page with all products of the category (the same as 2)
  4. /[productes]/[category]/[product]/ a single-productes.php that will show the category product
  5. /[productes]/[category]/[subcategory]/[product]/ a single-productes.php that will show the subcategory product (same as 4)

[category]/[subcategory] is an hierarchical taxonomy is equivalent to %categories_productes%

I have these two rules:

add_filter('rewrite_rules_array', 'tatanet_rewrite_rules');
function tatanet_rewrite_rules($rules) {
  $newRules  = array();
  $newRules['(.+)/?$']                                  = 'index.php?pagename=$matches[1]';
  $newRules['productes/(.+?)/([^/]+)(/[0-9]+)?/?$']     = 'index.php?productes=$matches[3]';
  return array_merge($newRules, $rules);

I have registered this taxonomy:

$labels = array(
'name'                          => 'Categories',
'singular_name'                 => 'Categoria',
'search_items'                  => 'Buscar Categories',
'all_items'                     => 'Totes les Categories',
'parent_item'                   => 'Categoria superior',
'edit_item'                     => 'Editar Categoria',
'update_item'                   => 'Actualitzar Categoria',
'add_new_item'                  => 'Afegir nova Categoria',
'new_item_name'                 => 'Nova Categoria',
'separate_items_with_commas'    => 'Separar Categories amb comes',
'add_or_remove_items'           => 'Afegir o treure Categoria',
$args = array(
'label'                         => 'Categories',
'labels'                        => $labels,
'public'                        => true,
'hierarchical'                  => true,
'show_ui'                       => true,
'show_in_nav_menus'             => true,
'query_var'                     => true,
'args'                          => array( 'orderby' => 'term_order' ),
'rewrite'                       => array( 'slug' => 'productes', 'with_front' => false, 'hierarchical' => true )
register_taxonomy( 'categories_productes', 'productes', $args );

I have registered this post type:

$labels = array(
  'name' => _x('Productes', 'post type general name'),
  'singular_name' => _x('Producte', 'post type singular name'),
  'add_new' => _x('Afegir nou', 'productes'),
  'add_new_item' => __('Afegir nou producte'),
  'edit_item' => __('Editar producte'),
  'new_item' => __('Nou producte'),
  'all_items' => __('Tots els productes'),
  'view_item' => __('Veure producte'),
  'search_items' => __('Cercar productes'),
  'not_found' =>  __('No s\'han trobat productes'),
  'not_found_in_trash' => __('No s\'han trobat productes a la Paperera'), 
  'parent_item_colon' => '',
  'menu_name' => 'Productes'
$args = array(
  'labels' => $labels,
  'public' => true,
  'publicly_queryable' => true,
  'show_ui' => true, 
  'show_in_menu' => true, 
  'query_var' => true,
  'capability_type' => 'post',
  'has_archive' => 'productes', 
  'hierarchical' => false,
  'menu_position' => -4,
  'rewrite' => array( 'slug' => 'productes/%categories_productes%', 'with_front' => false, 'hierarchical' => true ),
  'supports' => array( 'title' )
register_post_type( 'productes', $args );

This is the URL and the page load result:

  1. /productes/ OK => load the page products that runs template-productes.php
  2. /productes/[category]/ OK => load taxonomy-categories_productes.php
  3. /productes/[category]/[subcategory]/ ?? => 404 error not found
  4. /[productes]/[category]/[product]/ OK => load single-productes.php of the [product]
  5. /[productes]/[category]/[subcategory]/[product]/ OK => load single-productes.php of the [product]

Why the 3 point isn't working?

I have got a page named productes (maybe it is causing the conflict?)

I have got this permalink rewriter of the posts:

add_filter('post_link', 'categories_productes_permalink', 10, 3);
add_filter('post_type_link', 'categories_productes_permalink', 10, 3);

function categories_productes_permalink($permalink, $post_id, $leavename) {
  if (strpos($permalink, '%categories_productes%') === FALSE) return $permalink;
  $post = get_post($post_id);
  if (!$post) return $permalink;
  // get taxonomy terms
  $terms = wp_get_object_terms($post->ID, 'categories_productes');  
  if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
  else $taxonomy_slug = 'uncategorized';
  return str_replace('%categories_productes%', $taxonomy_slug, $permalink);
  • I'm still looking for an answer to my problem. Now I have to also keep in mind that the products within a category can be paged. Then there is a new rule for paged categories... Please someone help me? – jdivins Jul 27 '12 at 9:04

When Wordpress parses the incoming requests, it has no way of differentiating between what might be a product and what might be a subcategory. One will take precedence - it will either always look for a matching subcategory or it will always look for a matching product, depending on the order in which it processes the rules.

  • Then is there a way to change the order? Or a way to solve this? – jdivins Jul 16 '12 at 16:21
  • If you change the order you're just swapping precedence, you still won't get both to co-exist. the bottom line is each rule must have uniqueness baked in for WordPress to differentiate. – Milo Jul 27 '12 at 15:16

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.