• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2787

Original Genesis & WooCommerce Tutorials & 6000+ Guaranteed Code

Snippets

  • Try Premium
  • Log in

Add Custom Post Type (CPT) Pages To Posts Page

By default, only posts are displayed on your posts page which can either be your home page or blog page depending on your Reading Settings.

Reading Settings

If your theme includes custom post types (CPT), you can also add the single pages assigned to your CPT to your posts page as well.

Simply add this PHP code to the end of your child themes functions.php file and change the name portfolio to the name of your CPT.

add_action( 'pre_get_posts', 'add_custom_post_types_to_loop' );

function add_custom_post_types_to_loop( $query ) {
	if ( is_home() && $query->is_main_query() )
		$query->set( 'post_type', array( 'post', 'portfolio' ) );
	return $query;
}

You can add multiple CPT’s to this array like this.

add_action( 'pre_get_posts', 'add_custom_post_types_to_loop' );

function add_custom_post_types_to_loop( $query ) {
	if ( is_home() && $query->is_main_query() )
		$query->set( 'post_type', array( 'post', 'portfolio', 'video' ) );
	return $query;
}

Any custom taxonomy types you add to your CPT will automatically be included in your loop so you don’t need to add them in the array.

You can also add static pages to your posts page if you really want to simply by adding ‘page’ to the array in the above code snippets.

Add Custom Post Type to Archive Page

Change the is_archive() conditional tag to any other like is_category().

add_action( 'pre_get_posts', 'add_custom_post_types_archive' );

function add_custom_post_types_archive( $query ) {
	if ( $query->is_main_query() && !is_admin() && is_archive() )
		$query->set( 'post_type', array( 'post', 'your-cpt' ) );
	return $query;
}

Simply change your-cpt to the name of your custom post type.

More Code Using Pre Get Posts

  • Include Different Post Types In Your Home Page Loop
  • Customize Archive Pages Conditionally Using Pre Get Posts
  • Add Custom Post Type To RSS Feed

Custom Post Type pre_get_posts

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags
 

Loading Comments...
 

You must be logged in to post a comment.