php. Create guest author page via

Custom fields don’t have a collection template, so you might consider using a custom taxonomy, where the terms are the names of your guest authors.

I find it faster and easier than messing with custom templates on pages, with less code wrangling and complexity.

Naturally if you already have a lot of posts with your custom guest meta data, it will be a bit of a pain to switch things up. If you are in the early stages of implementation, I would encourage you to use a taxonomy instead as you will get collection view functionality baked in (the same way you do with categories).

Registering a custom taxonomy can be done manually with code, eg (adjusted 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 recommend Custom Post Type UI, I actually used it to create test data and the above code (saved me a bunch of time).

Add some conditions under Guest author taxonomy, which you can do while editing/creating the post and select an appropriate guest author. You will now have the ability to use Taxonomy View to list posts by your “guest” authors by querying yoursite/guest_author/name/ or click view Link under the word (guest author) to your guest author classification page in the admin. if you are thinking guest_author The url part looks awful, use something good looking in the rewrite part of the taxonomy.

Should you still want to use your original author rewrite filter, it will only require a small change to call it wp_get_post_terms Instead get_post_meta, Different get_post_meta There’s no single value return, but you can make it return a simple array of names that will cover both a single word and a multi-period result if we say implode, in a situation where you’ve added two or more guest authors. To a post it will become a comma-separated string and still look clean, and otherwise flatten to a simple non-comma-separated value.

$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);

Leave a Comment