0

I'd like to use two similar URL structures for the default 'post' type and a custom post type, the latter including a custom taxonomy (for language):

  • 'post' permalink structure is: article/%postname%

  • 'post_other' permalink structure is: article/%language%/%postname%

I've set up the 'post_other' permalink following these instructions, and it works. However, in my use case, it breaks 'post' links (links look OK on WP Admin, but I get a 404 error when opening them). I understand it would be problematic to use the very same slug for both, but I wonder if there's some way to make this work as they're not exactly the same. I don't have any issue if I change the 'post_other' permalink structure to article_whatever/%language%/%postname%.

Any clue on how to fix it?

This is the code on my functions.php file:

add_action('init', 'rewrites');
function rewrites() {
    add_rewrite_tag('%language%', '([^&]+)', 'language=');
    add_rewrite_rule('^/article/([^/]*)/([^/]*)/?','index.php?language=$matches[1]&name=$matches[2]&post_type=post_other','top');
}

function post_other_link( $post_link, $id = 0 ){
    $post = get_post($id);
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, 'language' );
        if( $terms ){
            $post_link = str_replace( '%language%' , $terms[0]->slug , $post_link );
            return $post_link;
        }
    }
    return $post_link;  
}
add_filter( 'post_type_link', 'post_other_link', 1, 3 );

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.