0

I am building a website that has many guest authors, but there is only one admin account that publishes it all.

Instead of creating multiple accounts, which would be impractical due to the high number of authors, I added a custom field called guest-author to every post and I managed to displayed it dinamically in the frontend.

Later, to change the author in the WordPress panel to show the guest author’s name, I added this to my functions.php:

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 = get_post_meta( $post->ID, 'guest-author', true );
 
    if ( $author )
    $name = $author;
    return $name;
}

But the problem is that there is not an individual page for each of the guest authors. Is there a way to create via php an author page for each guest author, automatically?

Thanks a lot!

1 Answer 1

0

Custom fields don't have an archive template so you may wish to consider using a custom taxnomy, where the terms are the names of your guest authors.

I think this is quicker and easier than messing around with custom templates on pages, less code wrangling and complexity.

Naturally if you have lots of posts with your custom guest meta data attached to them already, this is going to be a bit of a pain to switch things over. If however you're in the early stages of implementation i'd encourage you to use a taxonomy instead because you'll get archive view functionality baked in (in the same way you do with categories).

Registering a custom taxonomy can be done manually with code, like so (adjust as desired).

function wpse410299_register_my_taxes() {
    /**
     * Taxonomy: Guest Authors.
     */
    $labels = [
        "name" => esc_html__( "Guest Authors", "twentytwentytwo" ),
        "singular_name" => esc_html__( "Guest Author", "twentytwentytwo" ),
    ];
    
    $args = [
        "label" => esc_html__( "Guest Authors", "twentytwentytwo" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => [ 'slug' => 'guest_author', 'with_front' => true, ],
        "show_admin_column" => false,
        "show_in_rest" => true,
        "show_tagcloud" => false,
        "rest_base" => "guest_author",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "rest_namespace" => "wp/v2",
        "show_in_quick_edit" => false,
        "sort" => false,
        "show_in_graphql" => false,
    ];
    register_taxonomy( "guest_author", [ "post" ], $args );
}
add_action( 'init', 'wpse410299_register_my_taxes' );

Or if you prefer less code management and want a simple dashboard interface for creating taxonomies and post types, i'd recommend Custom Post Type UI, i actually used it to create test data and the code above(saved me a bunch of time).

Add some terms under the Guest Author taxonomy which you can do whilst you edit/create posts and select an appropriate Guest Author. You will now have the ability to use taxonomy views to list out posts by your "guest" authors by querying yoursite/guest_author/name/ or click the view link under the term(guest author) on your Guest Authors taxonomy page in the admin. If you're thinking the guest_author portion of the URL looks awful, use something nicer looking in the rewrite portion of the taxonomy.

Should you want to still make use of your original author rewrite filter it would only require a small change to call wp_get_post_terms instead of get_post_meta. Unlike get_post_meta there's no single value return, but you can have it return a simple array of names which will cover both a single term and multi term result if we call implode, so in the event there's a case where you've attached two or more guest authors to a post it will become a comma separated string and still look tidy, and just flatten to a simple non comma separated value otherwise.

$author = wp_get_post_terms( $post->ID, 'guest_author', array( 'fields' => 'names' ) );
$author = implode( ', ', $author );

Hope that helps.

For your follow-up request:

add_filter( 'author_link', 'change_author_to_term' );

function change_author_to_term( $link ) {
    global $post;
    $guest = get_the_terms($post->ID,'guest_author');
    if( !$guest )
        return $link;
    return get_term_link($guest[0]->term_id);
}
13
  • Perfect, thank you so much for taking the time and answering it so well. You're great!
    – usina
    Oct 11, 2022 at 1:36
  • You're welcome. :)
    – t31os
    Oct 13, 2022 at 22:00
  • Hi t31os, is it possible to expand this filter so it changes the author to the guest author in other instances as well? For example, in some plugins, when I try to fetch the author of the post, it shows my user login as the author, i.e, admin. Or when I try to fetch the author URL, it also shows the user login as the url. Thanks
    – usina
    Oct 24, 2022 at 13:25
  • Added example filter to change the author link to a term link. Assumes the first returned result is the correct one, and if none falls back to not changing the output.
    – t31os
    Oct 24, 2022 at 18:29
  • Thank you, it works perfectly, you are an angel. If you want to post on the other post I created I can mark you answer as right again. Thanks so much I hope I can help someone like you've helped me!
    – usina
    Oct 25, 2022 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.