WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Order by page__in array sequence? (10 posts)

  1. SpankMarvin
    Member
    Posted 3 years ago #

    Hi there

    I am feeding a list of page IDs into a custom query, and would like to have the pages listed in the same order as they appear in the array. I am aware I could probably do a foreach on the array and within that loop, run get_page, but I'm wondering if it's possible to call a loop using page__in and order as per the array of IDs? This would be the ideal situation. If not I'll just go with the foreach / get_page combo.

    Thanks in advance for any advice,

    John

  2. vtxyzzy
    Member
    Posted 3 years ago #

    Here is a sample script that should get you started. The list of IDs is kept in an array and passed as an argument to the query.

    After the query, an index of IDs is created with their index in the $wp_query->posts array. Then, a new $wp_query->posts array is created in the desired order.

    [Code moderated as per the Forum Rules. Please use the pastebin]

  3. SpankMarvin
    Member
    Posted 3 years ago #

    Fantastic. This is precisely what I was looking for in the absence of a parameter to do the hard work for me.

    I'll give this a try later tonight. Thank you for your time on this.

    John

  4. SpankMarvin
    Member
    Posted 3 years ago #

    Oh, it seems to have removed the code...

  5. alturnwall
    Member
    Posted 2 years ago #

    @vtxyzzy, could you possibly paste that code again since it's been removed? Thanks!

  6. vtxyzzy
    Member
    Posted 2 years ago #

    I'll have to recreate it, but will post a link when done.

  7. SpankMarvin
    Member
    Posted 2 years ago #

    I have a copy of the code. To save you the bother:

    Sorry, edit: Here is the pastebin link

  8. vtxyzzy
    Member
    Posted 2 years ago #

    Here you go - new and improved:

    http://pastebin.com/3vwiDSfb

  9. alturnwall
    Member
    Posted 2 years ago #

    Ah, you guys are amazing!

    I used @vtxyzzy's latest pastebin since it was a tad simpler and it works like a charm.

    Thank you!

  10. vtxyzzy
    Member
    Posted 2 years ago #

    If your problem has been solved, please use the dropdown on the right to mark this topic 'Resolved' so that anyone else with this question can see that there is a solution.

Topic Closed

This topic has been closed to new replies.

About this Topic