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

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:

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

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

Leave a Comment