Wordpress - How to use a custom post type archive as front page?
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;
}
Re-name your CPT archive to home.php
Then use pre_get_posts to alter the home page query so only CPT's display
function wpsites_home_page_cpt_filter($query) {
if ( !is_admin() && $query->is_main_query() && is_home() ) {
$query->set('post_type', array( 'your-cpt' ) );
}
}
add_action('pre_get_posts','wpsites_home_page_cpt_filter');
Replace your-cpt with the name of your custom post type.