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've been trying to implement some of the ideas discussed by Andrew Nacin http://wordpress.tv/2012/06/15/andrew-nacin-wp_query/ into my workflow, specifically trying to move away from using query_posts or a new WP_Query and altering the main query instead. So far I have this working on my homepage pretty well using the following code:

function some_name($query) {
    if ( $query->is_main_query() && $query->is_home() ) {
        $query->set( 'post_type', array( 'home_portfolio' ) );
    }
}
add_action( 'pre_get_posts', 'some_name');

This simply changes the default query's post_type to home_portfolio. I had to use the default index.php file and not select a page to use as the front page in order to get this to work.

I would like to use this method on certain pages too, but I'm unsure how to target the pages from functions.php. if I use $query->is_page(ID) the standard loop on the page returns a 404 whenever I try and change the post_type.

In other words, it seems to me that I should just be able to replace $query->is_home() with $query->is_page(ID) in functions.php and I should be able to target that specific page ID however I can't get it to work.

Any help would be greatly appreciated. Otherwise I'm being really efficient on my home page but all other pages are still running multiple queries (compared to modifying the main query on those pages).

share|improve this question
add comment

1 Answer

The is_home() conditional returns true when the currently displayed page is the blog posts index. If you want to target the site front page specifically instead, you need to use is_front_page():

function wpse83754_filter_pre_get_posts( $query ) {
    if ( $query->is_main_query() && is_front_page() ) {
        $query->set( 'post_type', array( 'home_portfolio' ) );
    }
}
add_action( 'pre_get_posts', 'wpse83754_filter_pre_get_posts' );

Also, you don't have to append $query-> to calls to is_home(), is_front_page(), etc. You do need to do so for is_main_query(), since you want to ensure that the query being filtered is specifically the main query, since any given page will have multiple queries.

So, if you want to target a specific page, just call is_page( $id ):

function wpse83754_filter_pre_get_posts( $query ) {
    if ( $query->is_main_query() && is_page( $id ) ) {
        $query->set( 'post_type', array( 'home_portfolio' ) );
    }
}
add_action( 'pre_get_posts', 'wpse83754_filter_pre_get_posts' );
share|improve this answer
    
Thanks for the trip about not having to append the $query-> calls. I tried targetting a specific page ID with your method, but when I try to load the page the 404.php template is loaded. However I think I was asking the wrong question for myself, I just need to use a secondary loop with WP_Query instead of trying to alter the main query in this case, since I need the page title info from it. I was using query_posts but I understand its better to use a new WP_Query. –  user1134053 Jan 30 '13 at 15:17
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.