$args = array( 'post_type' => 'product', 'posts_per_page' => 10 ); $custom_query = new WP_Query( $args ); if( $custom_query->have_posts() ) { while ( $custom_query->have_posts() ) { $custom_query->the_post(); // Display the custom query results } } wp_reset_postdata(); // Check if the current query is the main query if( is_main_query() ) { // Display the main query results }
function custom_main_query( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'post_type', array( 'post', 'page', 'product' ) ); $query->set( 'posts_per_page', 15 ); } } add_action( 'pre_get_posts', 'custom_main_query' );In this example, we are using the is_main_query function to check if the current query is the main query and then we are customizing the main query for the home page by adding additional post types and changing the number of posts per page. The is_main_query function is part of the WordPress core and is included in the WP_Query class.