Last active
September 20, 2017 23:29
-
-
Save wpscholar/2068729 to your computer and use it in GitHub Desktop.
Show WordPress custom post types on the main blog and archive pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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