Sometimes you might have a custom category post type archive that you want to display as your homepage. Your blog page might already be “occupied” by a link that lists all your blog items possibly including the custom post type items as well. Surely, you can add a category menu item to your main navigation menu but you would have to add a redirect from that link to your homepage and that’s not a good idea for performance and SEO purposes. Just a clarification, by homepage, I mean the page that gets loaded when users first visit you website URL and can be set at Settings->Reading->Homepage, not to be mistaken by the blog page that can also be set on the same screen.
So the process to achieve the above would be to create a page that would act as your home page and then setting that page as your homepage like the screenshot above. In my case, I have created a Videos page for a video custom post type. Then you would add the following code inside your functions.php file or though a code snippet.
function prefix_downloads_front_page( $query ) { // Only filter the main query on the front-end if ( is_admin() || ! $query->is_main_query() ) { return; } global $wp; $front = false; // If the latest posts are showing on the home page if ( ( is_home() && empty( $wp->query_string ) ) ) { $front = true; } // If a static page is set as the home page if ( ( $query->get( 'page_id' ) == get_option( 'page_on_front' ) && get_option( 'page_on_front' ) ) || empty( $wp->query_string ) ) { $front = true; } if ( $front ) : $query->set( 'post_type', 'video' ); $query->set( 'page_id', '' ); // Set properties to match an archive $query->is_page = 0; $query->is_singular = 0; $query->is_post_type_archive = 1; $query->is_archive = 1; endif; //Fix pagination if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; } $query->set( 'paged', $paged ); } add_action( 'pre_get_posts', 'prefix_downloads_front_page' );
Just replace video with your custom post type slug at line 24. The archive is then going to be available as your homepage or from clicking your homepage website logo (if you have one).
TIP: To change the homepage archive layout to something else, let’s say a column layout style layout, you would use the below code inserted with the same method as previously:
add_filter( 'generate_blog_columns','gm_portfolio_columns' ); function gm_portfolio_columns( $columns ) { if ( is_category( 'videos' ) || is_archive('video') ) { return true; } return $columns; } add_filter( 'generate_blog_get_column_count','gm_search_column_count' ); function gm_search_column_count( $count ) { if ( is_category( 'videos' ) || is_archive('video') ) { return 20; } return $count; }
In this case, I already had a column layout but with 4 columns. Inserting the code above would make my layout span 5 columns. I have In this case, I already had a column layout but with 4 columns. Inserting the code above would make my layout span 5 columns. I have assigned my video custom post type to a category of videos. The code above would look for a page where the category of videos is present, in this case if the video category was displayed is_category( 'videos' )
. At the same time though, we are also looking for the custom post type archive which is loaded as our homepage is_archive('video')
. Those two conditions would make sure that the correct layout would be loaded on both places. This solution works well for all kinds of paginated archives as well!
Have a look at this website here in which I have implemented the solution. I would also like to thank Tom Usborne from GeneratePress for corrections on the code!
I provide advanced customization work for GeneratePress, so please get in touch if you need any work done.
That’s all for now, hope you find that useful!
Works quite well, juste having issue with breadcrumbs (Yoast)
I think there would be some problems with Yoast. I think you can now achieve that with the GenerateBlocks plugin without using the code from the tutorial. Just create a static page and insert a GB query loop. Set your parameters accordingly and then set that page to be your homepage.
Hi @George Mitropapas,
I have another issue… The pagination isn’t working. When I click on page 2, I end up on a 404…
This article is old, will update it soon. The better way is to create a page and just add a GenerateBlocks query loop and set it up accordingly. Then set that page as your homepage.