Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have an array of post ids which I'm passing to query_posts using 'post__in'.

My question is how can I use the order from my array rather than ordering by 'date' etc?

share|improve this question
1  
order by what?? –  Bainternet Aug 11 '11 at 17:31
    
do the answers to this question help? [wordpress.stackexchange.com/questions/27832/… –  djb Oct 22 '11 at 10:52
add comment

1 Answer

You would need to build your own query and pass it to query_posts i.e.

if your array of post ids is:

array (10,21,1)   // corresponding to post ids. 

then you can query this way:

select * from wp_posts where `ID` in (10, 21, 2)
order by field(ID,10,21,2)

This will give you a result set in the order of your post ids array.

HTH

share|improve this answer
    
This is a great way to accomplish this. If you don't want to sacrifice the benefits of using query_posts, you can also chain into the posts_orderby filter to alter the orderby and use FIELD(ID, ...). Furthermore, to keep it dynamic, you can use, for a given array $arr, 'FIELD(ID, '.implode(',',$arr).')'. See the Codex for a good writeup of the posts_orderby filter –  Matthew Boynes Jan 28 '12 at 15:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.