Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to have a way of displaying a custom post archive for a particular author. I'm able to display custom posts for a category but not just the custom posts for a author. I already have a separate author page template displaying all the posts they have written divided by post type. I'm querying this using :

query_posts( 
  array( 
    'post_type' => 'custom_post_name', 
    'author'=>$curauth->ID 
  ) 
); 
while (have_posts()) : the_post();

Essentially I want a number of author templates pages for every author.

share|improve this question
    
It isn't clear what you want. You have an author template but you also want several other author templates per author? What do these templates do? How are they different from the author template you have? –  s_ha_dum Dec 17 '12 at 14:22
    
Seems like Josh want one author template for each post type. is that Josh? –  M-R Dec 17 '12 at 14:41
    
M-R that is it. A general author template(which is done) and one author template per post type. –  Josh Dec 17 '12 at 15:02

1 Answer 1

Three steps need to be followed to accomplish it.

1. Add rewrite rules

add_action('generate_rewrite_rules', 'author_cpt_add_rewrite_rules');
function author_cpt_add_rewrite_rules( $wp_rewrite ) 
{
  $new_rules = array( 
     'author/(.+)/(.+)' => 'index.php?author='.$wp_rewrite->preg_index(1) .
                            '&post_type=' .$wp_rewrite->preg_index(2) );

  //​ Add the new rewrite rule into the top of the global rules array
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

2. Redirect to specific templates

function author_cpt_template_redirect() {
    global $wp_query;
    // check for the target request
    if (!empty($wp_query->query_vars['author']) && !empty($wp_query->query_vars['post_type'])) {
        // turn off 404 error
        $wp_query->is_404 = false;

        // include if template is available
        if(file_exists('author-'.$wp_query->query_vars['post_type'].'.php'))
            include('author-'.$wp_query->query_vars['post_type'].'.php');
        else if(file_exists('author.php'))
            include('author.php');
        else
            include('index.php');

        return;
    }

}

3. Query posts to populate the page or template

add_action('template_redirect', 'author_cpt_template_redirect', 1);
function query_author_cpts( $query ) {
    // check for the target request
    if (!empty($query->query_vars['author']) && !empty($query->query_vars['post_type'])) 
    {
        // query posts accordingly
        query_posts( array( 
                        'post_type' => $query->query_vars['post_type'],
                        'author_name' => $query->query_vars['author'],
                        'paged' => get_query_var( 'paged' ) )
                    );
    }
}
add_action( 'wp', 'query_author_cpts' );
share|improve this answer
    
Thanks M-R. I have added the code and create an author page for one of the custom post types author-custom_post.php but when I go to ?author=name&post_type=custom_post it keeps bringing back the default author.php page. Is there something that I'm doing wrong? –  Josh Dec 18 '12 at 11:10
    
are not you using %postname% permalink? For %postname% permalink, http://example.com/author/admin/post is working.. I did not tested with default permalink. –  M-R Dec 18 '12 at 11:26
    
I get a page not found with that path. –  Josh Dec 18 '12 at 11:29
    
you flushed the permalink? Admin->Setting->Permalink->save –  M-R Dec 18 '12 at 11:36
    
that's working but still reverting to author.php rather than author-custom_post_type.php –  Josh Dec 18 '12 at 12:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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