Access to Instance Variables from WP_Query

$query = new WP_Query( $args );

if ( $query->have_posts( $query->have_posts()) ) : while( $query->have_posts()) :
  $query->the_post();
  global $post;
  switch ( $query->current_post ) {

    case 0 :
      // first post object is in var $post;
      echo 'The first post title is ' . $post->post_title;
      break;

    case 1 :
      // because we are insed the loop we can use all the post template tags
      echo 'The second post title is ' . get_the_title();
      break;

    ....

  }
endwhile; endif;
wp_reset_postdata();

Leave a Comment