Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Edit:

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?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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).

share|improve this answer
    
Thank you, but not solved yet. I've updated my question. Do you have any idea of what I am doing wrong? –  Manolo Salsas 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 Salsas Dec 23 '13 at 12:44
add comment

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

Source

share|improve this answer
    
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 Salsas Dec 22 '13 at 13:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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