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'm doing some rewriting of the url for a taxonomy and custom post type based on some code I found on here. However on my custom post type archive url I'm getting a 400 Bad Request. I honestly just want to redirect it, but none of my attempts to do so seem to work. My url looks like:

http://example.com/products/%product_description%/

Here is my actual code:

register_post_type('products', array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'menu_icon' => 'dashicons-admin-plugins',
'capability_type' => 'page',
'hierarchical' => true,
'rewrite' => array('slug' => 'products/%product_description%', 'has_archive' => 'products'),
'query_var' => true,
'exclude_from_search' => false,
'supports' => array(
  'title', 'editor', 'revisions',
),
'show_in_menu'  => true,
'has_archive' => true,
  ));
}
add_action('init', 'cptProducts');


function taxProductDescriptions() {
  register_taxonomy('product-descriptions','products',
array(
  'hierarchical' => true,
  'labels' => array(
    'name' => _x( 'Product Descriptions', 'taxonomy general name' ),
    'singular_name' => _x( 'Product Description', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Product Descriptions' ),
    'all_items' => __( 'All Product Descriptions' ),
    'parent_item' => __( 'Parent Product Description' ),
    'parent_item_colon' => __( 'Parent Product Description:' ),
    'edit_item' => __( 'Edit Product Description' ),
    'update_item' => __( 'Update Product Description' ),
    'add_new_item' => __( 'Add New Product Description' ),
    'new_item_name' => __( 'New Product Description Name' ),
    'menu_name' => __( 'Product Descriptions' ),
  ),
  'rewrite' => array( 'slug' => 'products', 'hierarchical' => true, 'with_front' => true ),
  'query_var' =>true,
)
  );
}
add_action( 'init', 'taxProductDescriptions' );


add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
 function mmp_rewrite_rules($rules) {
$newRules  = array();
$newRules['products/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[3]'; // my custom structure will always have the post name as the 5th uri segment
$newRules['products/(.+)/?$'] = 'index.php?product-descriptions=$matches[1]';

return array_merge($newRules, $rules);
}

function filter_post_type_link($link, $post)
{
if ($post->post_type != 'products')
{
  $link = str_replace('%product_description%', '', $link);
  return $link;
}

if ($cats = get_the_terms($post->ID, 'product-descriptions'))
{
    foreach($cats as $cat){
       $slug .= $cat->slug . '/';
    }
    $link = str_replace('%product_description%', substr($slug, 0, -1), $link); // see custom function defined below
}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
share|improve this question

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.