Redirect Author Archive to Custom Post Type With Query Vars?

Question

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.

0
aquariums 1 year 2022-09-06T06:36:29-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse