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.
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.
You must be logged in to post a comment.
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:
…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:
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
See Accepted Order & Orderby Parameters for WPQuery function. There is only
ASC
andDESC
parameters are allowed fororder
.