0

I am trying to figure out how to replace the author archives page with a custom post type page if one exists for that author using query vars (or some other method if query vars isn't the best way) - I have hundreds of authors to add so redirecting each one manually is really not ideal.

This is a snippet that replaces category archives with a page if the page permalink structure matches - I basically would like to achieve the same thing but for author pages to be replaced by a custom post type (in this case "bio" is the slug).

add_filter('request', function(array $query_vars) {
  // do nothing in wp-admin
  if(is_admin()) {
    return $query_vars;
  }

// check if the query is for a category archive
    if(isset($query_vars['category_name']) && !isset($query_vars['name'])) {
    
    //generate the page path
    $pagename = 'category/' . $query_vars['category_name'];
    
    //attempt to load the page matching the $pagename slug
    $page = get_page_by_path( $pagename , OBJECT );

    if ( isset($page) ){
      // completely replace the query with a page query
      $query_vars = array('pagename' => "$pagename");
    }
  }

return $query_vars;
 

Any suggestions on customizing the code above to work for this? Maybe there is a better way of doing this? I think I also need to somehow fit in this but I can't seem to get the right order or syntax:

$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

Appreciate any ideas or even pointers to resources for what would teach me how to understand this better.

5
  • can you be more specific about how this bio custom post type works? How is an author connected to it? Is it the CPT's post author? Slug? ID? Can you fix the indenting in your code, it's very difficult to read, use the Edit link underneath the tags to update your question
    – Tom J Nowell
    Sep 6 at 12:31
  • Hi Tom, posts created under the bio post type will be the pages replacing each author's individual archive page. So, if my author is "Emily Dickinson", I would create a post under the bio post type titled "Emily Dickinson" that I can I customize and edit with blocks. The goal is to replace her author archive with that custom page. I will try editing the code to indent better but I was struggling to just get the editor to keep it wrapped in code. Appreciate your help!
    – aquariums
    Sep 6 at 12:48
  • If it helps for clarification, Emily would be a registered user/author and has nearly 1800 poems published, each posted as a post in the "poems" category. By having a custom archive page as a post type I can easily customize her page to be more user-friendly for readers to browse through 1800 poems. Other authors, such as Ralph Waldo Emerson may not have many poems, so instead we might want to feature their posts in the category of books or essays. Having their archives as a custom post type makes it way easier to customize each page for each author using the block editor.
    – aquariums
    Sep 6 at 13:14
  • Also - the actual "author" of all of the bio pages is not the author itself - they will be created + edited only by the admin users of the site. Permalink structure currently: Default WP Authors Archive: ourwebsite.com/author/emily-dickinson Author's Bio Page: ourwebsite.com/bio/emily-dickinson Basically, if a bio page exists, we want WP to display that instead of the default archive page. Hope that makes sense and I haven't made it more confusing by trying to explain!
    – aquariums
    Sep 6 at 13:28
  • The other logic for making the bio pages a custom post type is I can easily query/display each bio with other custom taxonomies on various pages of the site - so for example, display authors by birth year, birthplace, subjects, etc.
    – aquariums
    Sep 6 at 13:40

1 Answer 1

0

With some experimentation got this to successfully redirect the archive page to the bio page:

add_filter('request', function( array $query_vars ) {

    if ( isset( $query_vars['author_name'] ) ) {
        $bio = $query_vars['author_name'];

        $query_vars = array( 'pagename' => "$bio" );
    }

    return $query_vars;
} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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