Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Last active September 20, 2017 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpscholar/2068729 to your computer and use it in GitHub Desktop.
Save wpscholar/2068729 to your computer and use it in GitHub Desktop.
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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment