July 31, 2013 at 10:07 am #145279
I am looking for a way to iterate thru each page of a wordpress loop in reverse order.
I am NOT looking to have the whole loop start from the oldest post to the newest post. I just want each page of the “newest to oldest” posts to be displayed in the order of…
5th oldest post 4th oldest post 3rd oldest post 2nd oldest post 1st post – most new
so basically, take a 5-post-per-page WordPress blog, and just reverse the posts displayed there.
If I could reverse just those 5 latest posts, that should also reverse the order of the meta tags that the jQuery Pinify plugin is generating, putting them in the order that I want.
Couldn’t find any info on reversing the order of a loop on a single page. I ‘can’ use order=asc, but that reverse the WHOLE loop, which I do not want. Just the one, single page of 5 results (the top 5 latest posts).August 1, 2013 at 7:58 pm #145416
I think I’ve come up with an approach for you. Add this code in your theme before entering the loop.
global $wp_the_query; $loaded_posts = $wp_the_query->posts; $loaded_posts = array_reverse($loaded_posts); $wp_the_query->posts = $loaded_posts;
$wp_the_query is the global variable used by the loop and $wp_the_query->posts holds the actual posts found by the loop. If you reverse the order of posts then you reverse the display of posts for that page without actually changing the overall query – it should still go through pages from most recent to oldest, yet by reversing the loop on the page, the posts on the page will show oldest to newest.
To tidy it up, the above code could be reduced to two lines:
global $wp_the_query; $wp_the_query->posts = array_reverse($wp_the_query->posts);August 1, 2013 at 7:58 pm #145417
Accidental additional follow-up. Ignore this. :)
You must be logged in to reply to this topic.