Change post permalink structure (WordPress)

I want to change the default post permalink structure from (example.com/post-name) to (example.com/author_firstname-author_lastname/post-name).

I tried to add a new rewrite tag which I could use in the custom permalink structure to use the author first name and last name in the URL but it didn’t work (404 error page).

add_filter('post_link', 'rating_permalink', 10, 3);
add_filter('post_type_link', 'rating_permalink', 10, 3);

function rating_permalink($permalink, $post_id, $leavename) {
    add_rewrite_tag( '%pauthor%', '(.+)', 'pauthor=' );
    if (strpos($permalink, '%pauthor%') === FALSE) return $permalink;

    // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get Author Data
        $post_author = get_post_field( 'post_author', $post->ID );
        $user_info = get_userdata($post_author);
        $first_name = $user_info->first_name;
        $last_name = $user_info->last_name;
        $pauthor = $first_name.'-'.$last_name;


    return str_replace('%pauthor%', $pauthor, $permalink);
}   

any help!?

Read more here: Change post permalink structure (WordPress)

Leave a Reply

Your email address will not be published. Required fields are marked *