Displaying a Custom Post Type Archive on the Front Page

Most developers use a custom page template if they need to display a custom post types on the front/home page. This is fairly easy to do using a new WP_QUERY:

$args = array(
	'post_type' => 'download',
);
$downloads = new WP_Query( $args );

However, WordPress still treats this as a page rather than archive, which can be problematic if you have specific styles or scripts that only load on archives or rely on certain body classes. So I started to experiment with a pre_get_posts function. I found this actually works quite well, even with pagination and infinite scrolling scripts.

Here’s the Gist of it:

I borrowed the basic idea from this post on StackExchange, and then refined the home page detection from this great discussion from wpaustralia.

About Devin

I'm a WordPress developer based in Austin, Texas. I run a little theme shop called DevPress and work for a startup called Cratejoy. Find me on twitter @devinsays.

4 Responses

  1. dway

    Nice snippet thx !
    Unfortunatly, the pagination works only if you set the home page as a static page. The “if ( ( is_home() && empty( $wp->query_string ) ) ) {” should return nothing instead and let the blog posts be displayed normally.

  2. Hi! Thanks for this snippet.

    To make the navigation work, I had to edit line 18: instead of

    if ( ( is_home() && empty( $wp->query_string ) ) ) {

    write :

    if ( ( is_home() || is_front_page() && empty( $wp->query_string ) ) ) {

    :)

Leave a Reply