Reversing the order of posts AFTER the query is performed

I figured out what I was doing wrong. A simple beginners mistake.

Array_reverse was working properly, but I wasn’t then reassigning the reversed array back to the $home_shows WP_Query, hence not seeing any change.

Here is the answer, and my revised code.

<?php
                    $args = array(
                        'post_type' => 'show',
                        'posts_per_page' => 5,
                        'order' => 'DESC',
                    );
                    $home_shows = new WP_Query($args);
                    //reverse the order of the posts, latest last
                    $array_rev = array_reverse($home_shows->posts);
                    //reassign the reversed posts array to the $home_shows object
                    $home_shows->posts = $array_rev;
                ?>
                <?php $captions = array(); ?>
                <?php if ( $home_shows->have_posts() ) : ?>         
                    <?php while ( $home_shows->have_posts() ) : $home_shows->the_post(); ?>

Thanks for the replies, glad I figured this one out.

Leave a Comment