jimboobrien
9/20/2017 - 11:29 PM

Show WordPress custom post types on the main blog and archive pages

Show WordPress custom post types on the main blog and archive pages

<?php

/**
 * Show WordPress custom post types on the main blog and archive pages
 * 
 * @param WP_Query $query
 **/
function show_custom_post_types( $query ) {
  
  // Show all custom post types on main blog and archive pages
  if ( $query->is_main_query() && ( is_home() || ( is_archive() && !is_post_type_archive() ) ) ) {
    $custom_post_types = get_post_types( array( 
      'public' => true,
      '_builtin' => false,
    ) );
    $post_types = array_merge( array('post'), $custom_post_types );
    $query->set( 'post_type', $post_types );
  }
  
}

add_action( 'pre_get_posts', 'show_custom_post_types' );