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' );