How To Loop Through Post Types In WordPress

Trying to loop through a post type in WordPress? All you may need is to create a new WordPress query with specific arguments.

Looping through a post type

To loop through a post type, you need to use the code below to pass the loop’s arguments through to the new query.

$args = array(
     'post_type' => 'example_post_type',
     'posts_per_page' => -1, //for infinite posts
     'order_by' => 'menu_order',
     'order' => 'DESC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
     //example content
endwhile;
wp_reset_postdata();

Importance of resetting the WordPress post data

It’s important to use wp_reset_postdata(); after your while loop.

Doing so will ensure you have successfully reset your post data in order to not affect any further loops used throughout the rest of the page.