Orderby = none not working [duplicate]

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

Leave a Comment