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?

up vote 2 down vote accepted

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' );

I had to do the same for a customer and I have found two resources which helped me:

How do you use a CPT as the default home page?

http://wpquestions.com/question/show/id/2944

  • Both good resources, thanks. – Isaac Lubow Oct 13 '11 at 14:24
  • 5
    Answers should be more than plain links. They should actually be an answer instead of a route where someone will maybe find an answer. Please help preventing link rot, edit your answer and provide the needed information that helps the OP as well as later visitors to solve their problem. – kaiser Jun 30 '14 at 12:56

This is the solution that worked for me:

function add_<MYTPE>_to_dropdown( $pages, $r )
{
    if('page_on_front' == $r['name'])
    {
        $args = array(
            'post_type' => '<MYTYPE>'
        );
        $items = get_posts($args);
        $pages = array_merge($pages, $items);
    }

    return $pages;
}
add_filter( 'get_pages', 'add_<MYTPE>_to_dropdown' );

function enable_front_page_<MYTPE>( $query )
{
    if('' == $query->query_vars['post_type'] && 0 != $query->query_vars['page_id'])
        $query->query_vars['post_type'] = array( 'page', '<MYTPE>' );
}
add_action( 'pre_get_posts', 'enable_front_page_<MYTPE>' );

Just replace <MYTYPE> with your custom post type machine name

https://wordpress.org/plugins/mpress-custom-front-page/ seems to be implementing the correct logic in a clean manner.

Github https://github.com/wpscholar/mpress-custom-front-page/

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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