WordPress 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).
Post Short Link:
{ 7 comments… read them below or add one }
You are awesome! You have saved me so much time. Thank you.
My pleasure.
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…
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.
Wow!! That’s….incredible! *jumps for joy*
You’re going to find this hilarious…but I’ve just used this snippet
Haha, awesome. That’s what it’s here for.