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'd like to use a custom post type archive as a site's front page, so that

 http://the_site.com/

is a custom post type archive displayed according to my archive-{post-type}.php file.

Ideally I would like to alter the query using is_front_page() in my functions.php file. I tried the following, with a page called "Home" as my front page:

 add_filter('pre_get_posts', 'my_get_posts');
 function my_get_posts($query){
     global $wp_the_query;
     if(is_front_page()&&$wp_the_query===$query){
        $query->set('post_type','album');
        $query->set('posts_per_page',-1);
     }
     return $query;
 }

but the front page is returning the content of "Home" and seems to be ignoring the custom query.

What am I doing wrong? Is there a better way, in general, of going about this?

share|improve this question

2 Answers 2

up vote 13 down vote accepted

After you have set a static page as your home page you can add this to your functions.php and you are good to go. This will call the archive-POSTTYPE.php template correctly as well.

add_action("pre_get_posts", "custom_front_page");
function custom_front_page($wp_query){
    //Ensure this filter isn't applied to the admin area
    if(is_admin()) {
        return;
    }

    if($wp_query->get('page_id') == get_option('page_on_front')):

        $wp_query->set('post_type', 'CUSTOM POST TYPE NAME HERE');
        $wp_query->set('page_id', ''); //Empty

        //Set properties that describe the page to reflect that
        //we aren't really displaying a static page
        $wp_query->is_page = 0;
        $wp_query->is_singular = 0;
        $wp_query->is_post_type_archive = 1;
        $wp_query->is_archive = 1;

    endif;

}
share|improve this answer
    
You nailed it, dude! Terima kasih banyak! –  Isaac Lubow Oct 12 '11 at 3:09
    
This function needs if(is_admin()) return; at the very beginning, otherwise it messes with the admin area. –  brasofilo Sep 11 '13 at 0:01
    
Thanks a lot! It works like a charm :) –  gosukiwi Jul 9 at 18:45

Thanks for the answer ljaas—I was looking to solve this exact problem. In order to get the custom post type archive template to be called I had to add the following conditions:

$wp_query->is_post_type_archive = 1;
$wp_query->is_archive = 1;
share|improve this answer
1  
Hi Eli, welcome to WPSE. "Answers" are meant to answer the initial question (stackexchange sites are not threaded discussion forums). This would be a much better fit for a comment. –  Johannes Pille Jul 18 '13 at 23:31
    
Thanks for the clarification Johannes. That is what I thought, though I could not figure out how to comment on the answer as there is no 'add comment' feature available. Is this a time-sensitive feature, or am I blind? –  Eli Jul 20 '13 at 20:09

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.