How to change permalink structure for custom post type and it’s taxonomies?

Ok I think I might have a solution. I have no idea if this is the right way to accomplish this, but as for now it’s the only thing that seems to work.

add_filter('rewrite_rules_array', 'mmp_rewrite_rules');

function mmp_rewrite_rules($rules) {
    $newRules                               = array();
    $newRules['portfolio/(.+)/(.+?).html$'] = 'index.php?project=$matches[2]';
    $newRules['portfolio/(.+)/?$']          = 'index.php?project_category=$matches[1]'; 

    return array_merge($newRules, $rules);
}


add_filter('request', 'mmp_rewrite_request');

function mmp_rewrite_request($vars) {
    if (isset($vars['project_category'])) {
        if (strpos($vars['project_category'], "https://wordpress.stackexchange.com/") !== false) {
            $categories = explode("https://wordpress.stackexchange.com/", $vars['project_category']);
            $vars['project_category'] = $categories[count($categories) - 1];
        }
    }

    return $vars;
}

Leave a Comment