Learn how to add custom post types to your author archive pages in WordPress using PRE_GET_POSTS.

Add Custom Post Types to Author Archive Page

Written by in Tutorials |

WordPress Author ArchiveWordPress does not add custom post types to author archive pages by default. In fact, it doesn’t add pages as well. It only displays posts from the author in question.

By manipulating the pre_get_posts action we can add any custom post type we want to the author archive pages.

Adding Post Types Using pre_get_posts

First we need to open our themes functions.php file (custom_functions.php for Thesis users) and add the following:

function custom_post_author_archive($query) {
    if ($query->is_author)
        $query->set( 'post_type', array('wp_plugin_review', 'client', 'post') );
    remove_action( 'pre_get_posts', 'custom_post_author_archive' );
}
add_action('pre_get_posts', 'custom_post_author_archive');

That will make sure all normal posts as well as “wp_plugin_review” and “client” custom post types will show up on author archive pages.

If you want to add pages to the author archive page you can do the following instead:

function custom_post_author_archive($query) {
    if ($query->is_author)
        $query->set( 'post_type', array('post', 'page') );
    remove_action( 'pre_get_posts', 'custom_post_author_archive' );
}
add_action('pre_get_posts', 'custom_post_author_archive');

FYI: If you decide to add pages to the author archive page, chances are you’ll need to make some CSS changes to make sure they display identically to your post snippets (especially if you run the Thesis framework).

Enjoy this post? Consider sharing it:

  • http://geoill.com Will

    You are awesome! You have saved me so much time. Thank you.

    • http://designpx.com/ Jason Manheim

      My pleasure. :)

      • Phillip

        This is a great snippet, however, it’s not working for me for some reason….the only post_types I can get to show are ‘post’ and ‘page’…. Not sure what my problem is. I have tried it using every custom post type i have setup and none of them are working.

        • Phillip

          This is what i’m using…my custom post type is ‘blog-posts’
          If i switch ‘blog-posts’ to ‘post’ or to ‘page’ it shows those post types, but as it is set, it displays no posts.

          function custom_post_author_archive($query) {    if ($query->is_author)        $query->set( ‘post_type’, array(‘blog-posts’) );    remove_action( ‘pre_get_posts’, ‘custom_post_author_archive’ );}add_action(‘pre_get_posts’, ‘custom_post_author_archive’);

          • http://designpx.com/ Jason Manheim

            Too many variables to consider so I’m afraid I can’t give you a solid answer. Your CPTs could be setup incorrectly, other code/plugins could be conflicting in some way, etc.

          • Phillip Rauschkolb

             Thanks Jason – it was caused by a plugin – Co-Authors Plus…which allows multiple authors to be assigned to posts.

          • http://designpx.com/ Jason Manheim

            Ah, good to know. Glad you got it worked out.

  • Avinash D’Souza

    While I can’t use this myself(no need to, as of now), this is a very nifty trick indeed! I’ve gotta say that I just love the stuff that comes out of your blogs. It’s very indicative of the quality you put out, in general… :-)

    • http://designpx.com/ Jason Manheim

      Thanks, Avinash. I really appreciate that. We’d love to put out more tuts like the preceding one and hopefully in the coming months we’ll add a writer or 2 in order to make that happen.

      • Avinash D’Souza

        Wow!! That’s….incredible! *jumps for joy*

    • Avinash D’Souza

      You’re going to find this hilarious…but I’ve just used this snippet

      • http://designpx.com/ Jason Manheim

        Haha, awesome. That’s what it’s here for. :)

  • Pablo

    Thanks a lot dude!!! Your code sorted my problem out!! Greetings from Spain!!

    • http://designpx.com/ Jason Manheim

      Great!

  • Chozen

    Is it possible to take it even further by separating each post type into it’s own column?

    For I have 3 different post types “Music”, “Videos” and “Articles”

    Would there be a way to give each of those their own column or section on the author page?

    • http://designpx.com/ Jason Manheim

      Absolutely but that would entail creating a custom template (or custom loop if you’re using Thesis) for the author archive pages.

  • Andy

    This is a great snippet and works flawlessly on the author archives page, but it is causing trouble in the admin area.  Now when a user who has authored two different CPTs open a list of one of them the posts in both show up.  I have a Photos and a Recipes CPT.  If I view all Photos posts in the admin area, both Photos and Recipe posts show up in the list.  Any way you know to avoid this?

    Thanks!

    • http://designpx.com/ Jason Manheim

      If you don’t want that code to affect the admin area, just add an is_admin() check: 
      if ( !is_admin() && $query->is_author() )