To display custom post types in the main blog loop in WordPress, you can modify the main query using the pre_get_posts action hook. This allows you to include your custom post types alongside regular posts in the loop.
Here’s an example code snippet that demonstrates how to achieve this:
function wpsnippets_add_custom_post_types_to_loop( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'your_custom_post_type' ) );
}
}
add_action( 'pre_get_posts', 'wpsnippets_add_custom_post_types_to_loop' );
In this code snippet, we’re using the pre_get_posts action hook to modify the main query. The is_admin() check ensures that the modification only applies to the front-end of the website. The $query->is_main_query() check ensures that we’re modifying the main query and not any secondary queries.
Inside the function, we use the $query->set() method to set the post_type parameter to an array containing both the default 'post' type and your custom post type. Replace 'your_custom_post_type' with the slug of your custom post type.
By including your custom post type in the post_type parameter, it will be included in the main blog loop alongside regular posts.
This code snippet is useful when you want to display custom post types in the main blog loop, allowing you to showcase different types of content together. For example, if you have a custom post type for “Portfolio Items” and you want to display them alongside regular blog posts, this code snippet will help you achieve that.
Examples
Example 1: Display Custom Post Types in the Main Blog Loop
This use case demonstrates how to display custom post types in the main blog loop on a WordPress website. By default, the main blog loop only displays regular posts, but with a few lines of code, you can include custom post types as well.
function wpsnippets_add_custom_post_types_to_loop( $query ) {
if ( $query->is_main_query() && ! is_admin() && $query->is_home() ) {
$query->set( 'post_type', array( 'post', 'custom_post_type' ) );
}
}
add_action( 'pre_get_posts', 'wpsnippets_add_custom_post_types_to_loop' );
This code snippet adds a custom post type called “customposttype” to the main blog loop. It hooks into the pre_get_posts action and modifies the query to include the custom post type in the main loop. The is_main_query() and is_home() functions ensure that the modification only applies to the main blog loop on the homepage.
Example 2: Display Multiple Custom Post Types in the Main Blog Loop
This use case demonstrates how to display multiple custom post types in the main blog loop. You can include as many custom post types as needed by adding them to the array in the code snippet.
function wpsnippets_add_custom_post_types_to_loop( $query ) {
if ( $query->is_main_query() && ! is_admin() && $query->is_home() ) {
$query->set( 'post_type', array( 'post', 'custom_post_type1', 'custom_post_type2' ) );
}
}
add_action( 'pre_get_posts', 'wpsnippets_add_custom_post_types_to_loop' );
This code snippet adds two custom post types, “customposttype1″ and “customposttype2″, to the main blog loop. The post_type parameter in the set() function is an array that includes both regular posts and the custom post types. This modification ensures that all specified post types are displayed in the main loop on the homepage.
Example 3: Exclude Custom Post Types from the Main Blog Loop
This use case demonstrates how to exclude specific custom post types from the main blog loop. Sometimes, you may want to exclude certain custom post types from the main loop while still displaying regular posts.
function wpsnippets_exclude_custom_post_types_from_loop( $query ) {
if ( $query->is_main_query() && ! is_admin() && $query->is_home() ) {
$query->set( 'post_type', array( 'post' ) );
}
}
add_action( 'pre_get_posts', 'wpsnippets_exclude_custom_post_types_from_loop' );
This code snippet excludes a custom post type called “customposttype” from the main blog loop. By removing the custom post type from the post_type array in the set() function, only regular posts will be displayed in the main loop on the homepage.
