How to rewrite custom post type URL for multiple depths instead of one specific depth

After testing countless different structures i came up with this. It’s manual work specifying each category but gives me more control.

If someone comes up with a better solution, please let me know.

function mmp_rewrite_rules($rules) {

//Product structure
$newRules['product/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[4]';
//The above structure only works of the postname is the 5th uri segment

//It's more flexible to confugure the depths for each category below
$newRules['product/televisies/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[3]';
$newRules['product/sports/(.+)/?$'] = 'index.php?products=$matches[1]';

//Category rule
$newRules['product/(.+)/?$']                = 'index.php?pcategory=$matches[1]'; 

return array_merge($newRules, $rules);
}

Leave a Comment