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?
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 thearchive-POSTTYPE.php
template correctly as well.Re-name your CPT archive to home.php
Then use pre_get_posts to alter the home page query so only CPT’s display
Replace your-cpt with the name of your custom post type.
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:
This works better for me overriding both blog posts and static page in Settings > Reading > Front page displays:
I’m using it in conjunction with a template override using the filters
front_page_template
andhome_template
to return a custom template.For me it breaks the pagination : either you select the index or a static page as the home page, the pagination links shows up but when clicking on page 2 I get :
I think it needs some rewrite rules to catch the paged argument and pass it correctly.
Anyway, a custom template page should be the solution with some additional rewrite rules.