How to Display Custom Post Types on Homepage in WordPress

By ThemesDNA2 min read

Share:

By default, your WordPress website's blog homepage displays the "post" post type in reverse chronological order, with the most recently published "post" post type at the top. If you use custom post types, you may want to display recent posts of a custom type on your blog homepage with or without default "post" post type.

Let's say you have a custom post type called "books". You can display "books" post type on the homepage by choosing a code given below according to your needs and adding it to your theme's "functions.php" file.

Code 1:

Display "post" and "books" post types on the homepage:

function themesdna_custom_posts_types_on_homepage( $query ) {
    if( $query->is_main_query() && ! is_admin() && $query->is_home() ) {
        $query->set( 'post_type', array( 'post', 'books' ) );
    }
}
add_action( 'pre_get_posts', 'themesdna_custom_posts_types_on_homepage' );

Code 2:

Display "post" and "books" post types on the homepage, archive pages (category pages, tag pages,...), search pages:

function themesdna_custom_posts_types_on_nonsingular_pages( $query ) {
    if( $query->is_main_query() && ! is_admin() && ( $query->is_home() || $query->is_search() || $query->is_archive() ) ) {
        $query->set( 'post_type', array( 'post', 'books' ) );
    }
}
add_action( 'pre_get_posts', 'themesdna_custom_posts_types_on_nonsingular_pages' );
Premium WordPress themes One-time payment   /   Lifetime updates

Ready to Build?

Get the ThemesDNA bundle.

Get all premium ThemesDNA themes with lifetime updates and unlimited website usage for a one-time payment.

  • 56 premium themes
  • Lifetime updates
  • Unlimited websites
  • No renewals

The complete premium theme bundle

$75

One payment. No renewals.

Get the ThemesDNA Bundle — $75