如果查询只针对少数帖子,那么正如Alex链接的那样,您可以在php中排序。然而,这并不能很好地扩展。
正如Kovshenin所建议的,更好的选择是使用posts_orderby
过滤器:
$post_ids = array(83,24,106,2283,14);
$args = array(
\'post_type\' => \'post\',
\'post__in\' => $post_ids,
\'numberposts\' => 10,
);
//Callback to filter the ORDER BY part of the query
function wpse67823_orderby_post_in($orderby, $query){
global $wpdb;
//Remove it so it doesn\'t effect future queries
remove_filter(current_filter(), __FUNCTION__);
$post__in = implode(\',\',$query->get(\'post__in\'));
return "FIELD( {$wpdb->posts}.ID, $post__in )";
}
//Add filter and perform query
add_filter(\'posts_orderby\',\'wpse67823_orderby_post_in\',10,2);
$wpse67823_query = new WP_Query($args);
WordPress 3.5将看到接受的附加值
WP_Query
对于
orderby
: \'post\\uu in\'。查看此trac票据:
http://core.trac.wordpress.org/ticket/13729