WordPress post loop is basic need on WordPress website. You can use loop to display post from default post types, custom post types, tags, categories, post by users, new post ….

  • Custom WP

When you need to write custom loop code in WordPress, this is simplest way to do that.

<?php
    $args = array(
        'post_type' => 'post'
    );

    $post_query = new WP_Query($args);
       if($post_query->have_posts() ) {
          while($post_query->have_posts() ) {
             $post_query->the_post();
       ?>
         <h2><?php the_title(); ?></h2>
     <?php
      }
    }
?>
  

If you need to loop through custom post type, or to display 10 post in the loop, simple change arguments, like so:

  $args = array(
        'post_type' => 'my_custom_post_type'
         'posts_per_page' => '10',
    );

But if you need to loop through multiple post type, then you need to add array of arguments, like this:

  $args = array(
        'post_type' => array('post', 'my_custom_post_type', 'some_other_post_type),
         'posts_per_page' => '10',
    );

Loop WordPress
WordPress