Created
January 28, 2012 19:25
-
-
Save jmdodd/1695504 to your computer and use it in GitHub Desktop.
Add custom post types to the Loop by adding an action to pre_get_posts (doing it wrong)
This file contains 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 | |
if ( ! function_exists( 'ucc_include_custom_post_types' ) ) { | |
function ucc_include_custom_post_types( $query ) { | |
// Don't break admin or preview pages. This is also a good place to exclude feed with ! is_feed() if desired. | |
if ( ! is_preview() && ! is_admin() && ! is_singular() ) { | |
$args = array( | |
'public' => true , | |
'_builtin' => false | |
); | |
$output = 'names'; | |
$operator = 'and'; | |
$post_types = get_post_types( $args, $output, $operator ); | |
// Add 'link' and/or 'page' to array() if you want these included. | |
// array( 'post', 'link', 'page' ), etc. | |
$post_types = array_merge( $post_types, array( 'post' ) ); | |
if ($query->is_feed) { | |
// Do feed processing here if you did not exclude it previously. This if/else | |
// is not necessary if you want custom post types included in your feed. | |
} else { | |
$my_post_type = get_query_var( 'post_type' ); | |
if ( empty( $my_post_type ) ) | |
$query->set( 'post_type', $post_types ); | |
} | |
} | |
} } | |
add_action( 'pre_get_posts', 'ucc_include_custom_post_types' ); | |
/* | |
Copyright 2012 Jennifer M. Dodd <jmdodd@gmail.com> | |
Released under the GPLv2 (or later). | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment