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:

{ 32 comments… read them below or add one }

Will October 8, 2011

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

Reply

Jason Manheim October 10, 2011

My pleasure. :)

Reply

Phillip April 19, 2012

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.

Reply

Phillip April 19, 2012

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’);

Reply

Jason Manheim April 19, 2012

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.

Avinash D'Souza October 11, 2011

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… :-)

Reply

Jason Manheim October 11, 2011

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.

Reply

Avinash D'Souza October 12, 2011

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

Reply

Avinash D'Souza November 8, 2011

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

Reply

Jason Manheim November 8, 2011

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

Reply

Pablo March 8, 2012

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

Reply

Jason Manheim March 8, 2012

Great!

Reply

Chozen March 21, 2012

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?

Reply

Jason Manheim March 21, 2012

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

Reply

Andy May 13, 2012

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!

Reply

Jason Manheim May 13, 2012

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

Reply

Andy May 21, 2012

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.

Reply

Jason Manheim August 2, 2012

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

Reply

Dan May 21, 2012

¡Thank you SO MUCH for this!

Reply

meloniq September 14, 2012

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

Reply

Paul Grieselhuber October 30, 2012

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!!

Reply

Jason Manheim October 30, 2012

My pleasure!

Reply

Nick Powers November 24, 2012

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

Reply

Nick Powers November 24, 2012

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

Reply

Peter P December 21, 2012

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

Reply

Pat O'Brien March 14, 2013

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?

Reply

Pat O'Brien March 14, 2013

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

Reply

Henk van den Bor March 15, 2013

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

Reply

Chelle Stein March 26, 2013

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

Reply

Ikyo March 4, 2014

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

Reply

Phillip Rauschkolb April 27, 2012

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

Reply

Jason Manheim April 27, 2012

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

Reply

Leave a Comment