I want to include some kinds of posts in the default query. When the loop begins:

        if ( have_posts() ) :
            // Start the Loop.
            while ( have_posts() ) : the_post();

the default type post is 'post', so other kind of posts won't enter the loop (e.g. I have a custom post type called 'news').

I could use the WP Query object and do:

    $type = 'news';
    $args = array ('post_type' => $type);
    $temp = $wp_query; // assign ordinal query to temp variable for later use  
    $wp_query = null;

    $wp_query = new WP_Query($args); 

 while ( $wp_query->have_posts() ) : $wp_query->the_post();

And this will work. But is there any way to just let other kinds of posts to the main query object to don't worry about changing the loop everywhere?


I've also tried with pre_get_posts without success:

// Load Home Page Posts( reviews and news )
function search_home_posts( $query ) {
    if( $query->is_home() && $query->is_main_query() && !is_admin() ) {
        $query->set( 'post_type', array( 'reviews', 'news' ) );
add_action( 'pre_get_posts', 'search_home_posts' );

but doesn't do anything. I've tried more combinations like $query->set( 'post_type', 'reviews' );, but the same behavior. Any idea?


You should use pre_get_posts hook, which allows you to target one (or multiple) query precisely and change it so that everything works correctly (which is rarely result of manipulating main query in template, especially with pagination).

  • Thank you, but not solved yet. I've updated my question. Do you have any idea of what I am doing wrong? – Manolo Dec 23 '13 at 10:34
  • It was due to my home template. I forgot to delete the query object that I used before. It works perfect. Thank you. – Manolo Dec 23 '13 at 12:44

You could use this :

$query = new WP_Query( 'post_type=any' );

Retrieves any type except revisions and types with 'exclude_from_search' set to TRUE


  • This is showing the pages too. I just want to get all post types (not pages), or two types, ... I've read the documentation but don't make my mind clear. – Manolo Dec 22 '13 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.