Include Custom Post Types on the Archive Page

Modify Wordpress' main query to include other custom-post-types when viewing the archive page. In this example we include 'portfolio' and 'news' custom-post-types along with the default 'post' post-type.

// Add 'portfolio' and 'news' post types to archive page
add_action( 'pre_get_posts', 'add_custom_post_types_to_archive' );

function add_custom_post_types_to_archive( $query ) {
	if (!is_admin()){
	    if ( is_archive() && $query->is_main_query() )
	        $query->set( 'post_type', array( 'post', 'portfolio','news' ) );
	    return $query;

Posted In: Snippets     ||      Tags: Wordpress