I want to include custom post type in the author arhive page. I tried the method discussed in this post:
add_action( 'pre_get_posts', 'custom_post_author_archive' );
function custom_post_author_archive( &$query )
{
if ( $query->is_author )
{
$query->set( 'post_type', 'custom_name' );
remove_action( 'pre_get_posts', 'custom_post_author_archive' ); // run once!
}
}
But by this method, only the custom_name posts are queried. I want to query both the custom_name posts and the normal posts.
What changes will I have to make?
As Kaiser mentioned, the
post_type
parameter can accept an array of post types. Updating the function to useis_main_query()
to limit the filter to only the “main” query, the code would look like:Simply said, the
$query
argument insidepre_get_posts
refers to theWP_Query
class/object as you can see in core.That means that any callback/action attached to the hook can do exactly the same thing as
new WP_Query( [ etc. ] );
could do. Therefore you can use an array of post types as well.