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() )

      • Andy

        Thanks for the idea.  Unfortunately I have other users (subscribers and contributors) logging in and using these CPT’s from the admin panel.  I solved it by using this right before the loop in my author.php file:
        global $query_string;query_posts( $query_string . ‘&post_type[]=post&post_type[]=event&post_type[]=gg_recipes&post_type[]=gg_photos&posts_per_page=-1′ );where event, gg_recipes and gg_photos are my custom post types.

        • http://designpx.com/ Jason Manheim

          is_admin checks whether or not the dashboard or administration panel is being shown, not whether or not the user’s role is administrator.

  • http://www.camionetica.com Dan

    ¡Thank you SO MUCH for this!

  • meloniq

    Instead of removing function from ‘pre_get_posts’ action, You can use $query->is_main_query()

  • Paul Grieselhuber

    Thanks so much for this snippet. I’d just finished making outstanding looking author bio pages, then changed a certain subset of posts to a custom post type, all the sudden many author’s posts were not showing – and their bios where empty. This code was a life saver. THANKS!!

    • http://designpx.com/ Jason Manheim

      My pleasure!

  • http://www.facebook.com/drvpn Nick Powers

    I have been looking for an answer everywhere to no avail…

    It appears that you have to know all the post types you want to display using the method you have described above.

    Say I have a custom post type called mycoolthings, how do I add mycoolthings to the list, without knowing what the list currently is. Specifically I am writing a plugin that needs to add a custom post type and I don’t want to leave out any that are previously defined, through functions file or another plugin or theme. Is there way to get a list of what is already configured so I can just add mine?

    Thanks!

    Nick

    • http://www.facebook.com/drvpn Nick Powers

      I figured out, at least one way, to do it.

      if( isset( $query->query_vars["post_type"] ) ) {
      $cpt = $query->query_vars["post_type"];

      array_push( $cpt, ‘mycoolthings’ );

      $query->set( ‘post_type’, $cpt );
      } else {
      $query->set( ‘post_type’, array( ‘post’, ‘mycoolthings’ ) );

      }

      As long as everyone does this they should play well together… and if they don’t well, at least they are the one doing it wrong… LOL

      Nick

  • Peter P

    What would the code be to display custom post types for the logged in user on a custom page template?

  • http://www.patobrienphoto.com/ Pat O’Brien

    I’m planning on using the author template for more of a profile page for my users. Is it possible to remove all published posts and pages from this page so that I can display my custom content?

    • http://www.patobrienphoto.com/ Pat O’Brien

      I just used a custom loop for this. Seems to be working well.

      $loopty_loop = new custom_profile_page;
      class custom_profile_page extends thesis_custom_loop {
      function archive() {
      if (is_author()) {
      echo “Hello World!”;
      } else {
      thesis_loop::archive();
      }
      }
      }

  • Henk van den Bor

    Thanks for pointing me in the right direction…

    I ended up using the code like this in a Genesis child theme:

    function custom_post_author_archive($query) {
    if ( !is_admin() && $query->is_author() )
    $query->set( ‘post_type’, array(‘my-cpt’, ‘post’) );
    $query->is_main_query();
    }

  • http://www.facebook.com/chellestein Chelle Stein

    Thank you so much for this, worked like a charm :) I didn’t even have to tweak my theme’s CSS or archive page :)

  • Ikyo

    Thank you…thank you very much for that wonderful hint Jason !