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.

This question already has an answer here:

I'm trying to set 'orderby = none' to my loop but it is not working. Here's my code:

$query = new WP_Query(array('showposts'=>2, 'post__in' => array(99,4,5,2,8,55), 'orderby'=>'none'));

Could anyone help me?

Thanks.

share|improve this question

marked as duplicate by toscho Dec 28 '13 at 16:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
What exactly is not working ? is it not rendering posts, not displaying or throwing some error ? –  amit Aug 21 '12 at 16:58
    
it's sorting the posts and I need the posts ordered like 'post__in' order like this: 99,4,5,2,8,55. –  Eduardo Leal Aug 21 '12 at 17:15
    
I think there's similar question which has been already solved - wordpress.stackexchange.com/q/11055/17968 –  amit Aug 21 '12 at 17:29

2 Answers 2

up vote 3 down vote accepted

Order by none doesn't do what you think it does. If you don't specify an order, then MySQL doesn't guarantee any particular order and will simply get the posts in whatever order it has them. Since they were inserted in a particular order, then you'll probably get them in that order.

Since you need the posts in a specific order (in your case, by post IDs with "99,4,5,2,8,55"), then mysql isn't capable of that without a far more complex ORDERBY clause. Specifically, you'd have to use this with mySQL syntax:

ORDER BY FIELD($wpdb->posts.ID,99,4,5,2,8,55);

...and that's not particularly easy to do with the WordPress query engine.

You could use the posts_orderby filter to do something like this, with code like this:

function reorder_posts($orderby) {
    global $wpdb;
    return "ORDER BY FIELD({$wpdb->posts}.ID,99,4,5,2,8,55)";
}
add_filter('posts_orderby', 'reorder_posts');

You'd want to add the filter before your query, then remove it after, so as not to change anything else.

But this is a hard-coded solution, and you'd probably be better off finding a more generic approach than fiddling with the queries. Storing your order in metadata and then selecting based on that and ordering based on meta_value_num is possible with the WP_Query system.

Edit: Note, this just got pushed into WordPress 3.5, so when that comes out, you'll be able to use 'orderby'=>'post__in' (that's a double underscore there) and it will use the value from the post__in for the ordering.

Ticket here: http://core.trac.wordpress.org/ticket/13729

Patch here: http://core.trac.wordpress.org/changeset/21776

share|improve this answer
    
Thank's Otto! It Worked! –  Eduardo Leal Aug 22 '12 at 14:35

See Accepted Order & Orderby Parameters for WPQuery function. There is only ASC and DESC parameters are allowed for order.

$query = new WP_Query(array('showposts'=>2, 'post__in' => array(99,4,5,2,8,55), 'orderby'=>'none'));

Use 'orderby'=>'none' not 'order'=>'none'

share|improve this answer
    
I'm already using 'orderby', it was just an error while typing –  Eduardo Leal Aug 21 '12 at 16:53
    
And it's doesn't working too. –  Eduardo Leal Aug 21 '12 at 16:54
    
Will you update your question to correct code (Pleas copy-paste your exact code) –  amit Aug 21 '12 at 16:56

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