0

I am looking for a code that can change the permalink of all my post who have a certain Tag.

Regular permalink structure:

/%category%/%day%-%monthnum%-%year%/%postname%/

The permalink structure I want for the specific category:

/%postname%/

The following code I tried :

add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
    $tags = get_the_tags($post->ID); 
    if (  !empty($tags) && $tags[0]->name == "MYTAG" ) {
        $permalink = trailingslashit( home_url('/'. $post->post_name .'/' ) );
    }
    return $permalink;
}
add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
    $new_rules['^([^/]+)/?$'] = 'index.php?name=$matches[1]';
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite;
}

The code is working BUT now all my pages (except the home) are showing an error 404. I guess that my rules in "$new_rules" are not perfect.

Does someone has any clue ?

Best regards, Jonathan A.

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.