Skip to content

Instantly share code, notes, and snippets.

@LucaRosaldi
Forked from jmdodd/gist:1695468
Created December 17, 2012 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucaRosaldi/4317351 to your computer and use it in GitHub Desktop.
Save LucaRosaldi/4317351 to your computer and use it in GitHub Desktop.
WP: Add Custom Post types to main Query.
<?php
if ( ! function_exists( 'ucc_add_cpts_to_pre_get_posts' ) ) {
function ucc_add_cpts_to_pre_get_posts( $query ) {
if ( $query->is_main_query() && ! is_post_type_archive() && ! is_singular() && ! is_404() ) {
$my_post_type = get_query_var( 'post_type' );
if ( empty( $my_post_type ) ) {
$args = array(
'exclude_from_search' => false,
'public' => true,
'_builtin' => false
);
$output = 'names';
$operator = 'and';
$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_add_cpts_to_pre_get_posts' );
/*
Doing it wrong in so many different ways:
http://gist.github.com/1695492
http://gist.github.com/1695498
http://gist.github.com/1695504
*/
/*
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