Custom Taxonomy – Change get_author_posts_url/author URL via filter

I have a follow up question to this answer: php . Create guest author page via

I would like to modify the get_author_posts_url function to get the custom taxonomy guest author slug. I need this because right now the author url is not matching the name of the guest author slug, it is showing the slug with the name of the user who made the post (admin). So far I have taken the following steps:

  1. I used the plugin Custom Post Type UI. Have created a custom taxonomy for guest author (autoria) by using
  2. Then I used this filter to register the guest author as a post author in the WP panel:
add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
 
function guest_author_name( $name ) {
    global $post;
 
    $author = wp_get_post_terms( $post->ID, 'autoria', array( 'fields' => 'names' ) );
 $author = implode( ', ', $author );
    if ( $author )
    $name = $author;
    return $name;
}

So now I have guest author base and slug like mywebsite.com/autoria/guest-author, and it correctly displays the list of posts by that guest author.

Then I modified the author base with the following code:


 add_action('init','change_author_permalinks');  
 function change_author_permalinks()  
 {  
      global $wp_rewrite;  
      $wp_rewrite->author_base="autoria"; 
      $wp_rewrite->author_structure="https://wordpress.stackexchange.com/" . $wp_rewrite->author_base. '/%author%';  
 }  

The problem is that when I try to redirect it dynamically using Author_URL it doesn’t show the guest-author slug but the slug of the user who created the post, such as admin. So the only way I could fix this was to create a custom field called guest-author-url and then manually fill in all posts with this information to redirect to this custom field.

I’d like to change the following function to fetch the guest author taxonomy link instead, so it can be dynamic:

    function get_author_posts_url( $author_id, $author_nicename="" ) {
    global $wp_rewrite;

    $author_id = (int) $author_id;
    $link      = $wp_rewrite->get_author_permastruct();

    if ( empty( $link ) ) {
        $file = home_url( "https://wordpress.stackexchange.com/" );
        $link = $file . '?author=" . $author_id;
    } else {
        if ( "' === $author_nicename ) {
            $user = get_userdata( $author_id );
            if ( ! empty( $user->user_nicename ) ) {
                $author_nicename = $user->user_nicename;
            }
        }
        $link = str_replace( '%author%', $author_nicename, $link );
        $link = home_url( user_trailingslashit( $link ) );
    }

This would save me so many hours of manually filling in the guest authors links one by one.

thanks a lot for any help

Leave a Comment