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: