1. Querying by Post Type

In any template file of the WordPress theme system, you can also create new queries to display posts from a specific post type. This is done via the post_type argument of the WP_Query object.


$args = array( ‘post_type’ => ‘product’, ‘posts_per_page’ => 10 );

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();


echo ‘‘;


echo ‘‘;



This simply loops through the latest 10 product posts and displays the title and content of them one by one.



2. Custom Post Types in the Main Query

Registering a custom post type does not mean it gets added to the main query automatically.

If you want your custom post type posts to show up on standard archives or include them on your home page mixed up with other post types, use the pre_get_posts action hook.


// Show posts of ‘post’, ‘page’ and ‘movie’ post types on home page

add_action( ‘pre_get_posts’, ‘add_my_post_types_to_query’ );

function add_my_post_types_to_query( $query ) {

if ( is_home() && $query->is_main_query() )

$query->set( ‘post_type’, array( ‘post’, ‘page’, ‘movie’ ) );

return $query;



Leave a Reply