How to get wordpress main query posts as an array

I need to get the main query posts as an array. Example, in common tag page(tag.php), I need to get all the posts as an array ( like get_posts() do) and display it using some multiple loops instead of using default wordpress loop as shown under

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Related posts

Leave a Reply

1 comment

  1. $posts is the variable you’re looking for it. It is the equivalent of get_posts results for the main query. It’s in the global namespace, so in order to access it somewhere else you’ll need to use the keyword global.

    global $posts;
    foreach( $posts as $a_post ) {
        echo $a_post->post_title;
    }