Created
January 28, 2012 19:21
-
-
Save jmdodd/1695492 to your computer and use it in GitHub Desktop.
Add custom post types to the Loop (doing it wrong)
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 | |
| if ( ! function_exists( 'ucc_pre_get_posts_filter' ) ) { | |
| function ucc_pre_get_posts_filter( $query ) { | |
| if ( ! is_preview() && ! is_admin() && ! is_singular() && ! is_404() ) { | |
| if ( $query->is_feed ) { | |
| // As always, handle your feed post types here. | |
| } else { | |
| $my_post_type = get_query_var( 'post_type' ); | |
| if ( empty( $my_post_type ) ) { | |
| $args = array( | |
| 'public' => true , | |
| '_builtin' => false | |
| ); | |
| $output = 'names'; | |
| $operator = 'and'; | |
| // Get all custom post types automatically. | |
| $post_types = get_post_types( $args, $output, $operator ); | |
| // Or uncomment and edit to explicitly state which post types you want. */ | |
| // $post_types = array( 'event', 'location' ); | |
| // 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' ) ); | |
| $query->set( 'post_type', $post_types ); | |
| } | |
| } | |
| } | |
| } } | |
| add_action( 'pre_get_posts' , 'ucc_pre_get_posts_filter' ); | |
| /* | |
| 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