1

So I managed to get my rewrites to work finally. But since I disabled the slug that usually gets prepended for CPT's, I've got general rewrites, that ends up giving me a 404 when trying to browser pages.

What can I do to remedy this?

If I change from top to bottom I get the reversed problem, where pages work, but project and director does not.

add_filter('post_type_link', __NAMESPACE__ . '\\post_type_link', 1, 3);
function post_type_link( $link, $post = 0 ){
    if ( $post->post_type == 'project' ) {
        $director_id = get_field('director', $post->ID);
        $director = get_post($director_id);
        $director_name = $director->post_name;

        $url = home_url( $director->post_name . '/' . $post->post_name );
        return $url;
    } elseif ( $post->post_type == 'director' ) {
        $url = home_url( $post->post_name );
        return $url;
    } else {
        return $link;
    }
}


add_filter( 'query_vars', __NAMESPACE__ . '\\register_query_vars' );
function register_query_vars( $vars ) {
    $vars[] = 'director_name';
    return $vars;
}


add_action( 'init', __NAMESPACE__ . '\\rewrites_init' );
function rewrites_init(){
    add_rewrite_rule(
        '([^/]*)/([^/]*)/?',
        'index.php?post_type=project&director_name=$matches[1]&name=$matches[2]',
        'top' );

    add_rewrite_rule(
        '([^/]*)/?',
        'index.php?post_type=director&name=$matches[1]',
        'top' );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.