How to use a custom post type as front page?

I would like to set a site’s front page to be a single post from a custom post type. I have been able to alter the request for my front page to a Custom Post Type archive with the following code (originally posted here):

function custom_front_page($wp_query){
    if($wp_query->get('page_id')==get_option('page_on_front')){
        $wp_query->set('post_type','album');
        $wp_query->set('page_id',''); // empty
        // fix conditional functions
        $wp_query->is_page = false;
        $wp_query->is_archive = true;
        $wp_query->is_post_type_archive = true;
    }
}
add_action('pre_get_posts','custom_front_page');

Replacing

$wp_query->is_archive = true;
$wp_query->is_post_type_archive = true; 

with

$wp_query->is_single = true;

calls the single-album.php template as I’d like to, but it still returns ALL the posts in the “Albums” category, instead of only one.

Adding

$wp_query->set('posts_per_page',1);

has no effect.

What should I be doing instead?

Bonus question: is there a good reference somewhere about how to manipulate the query this way?

4 s
4

Easiest way to display single post on front page would be:

global $wp_query;
$wp_query = new WP_Query( array( 'p' => 'POST ID HERE' ) );
include( 'single-POSTTYPE.php' );

Leave a Comment