Skip to main content

Forums

This topic contains 2 replies, has 2 voices, and was last updated by  thechrisroberts 6 years, 3 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #145279

    macharborguy
    Participant

    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.

    I am using PHP + a secondary loop to generate the head javascript code for jQuery Pinify (for a pinned Windows 7 bookmark with dynamic links to the latests news posts). It appears that if I iterate normally thru the loop, the “jump list” on the pinned site’s menu displays with the fifth post being on the top of the list, and the first post being on the bottom of the list.

    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).

    #145416

    thechrisroberts
    Participant

    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);
    
    #145417

    thechrisroberts
    Participant

    Accidental additional follow-up. Ignore this. :)

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

icon-link icon-logo-star icon-search icon-star