Maybe you’re looking for the post__in
parameter in WP_Query
.
$query = new WP_Query(array(
'post__in' => array(23,18,2,199,6,8)
);
And then:
while ( $query->have_posts() ) {
$query->the_post();
/* post loop */
}
Take a look at the docs. =D
For public queries:
post__in
is not public queryable by default, so you can just validate and copy $_GET['post__in']
on the parse_query
action hook, and let the thing happen.
add_action('parse_query', 'wpse59828_parse_query');
function wpse59828_parse_query($query) {
if (empty($_GET['post__in']))
return $query;
$posts = explode(',', $_GET['post__in']);
$post__in = array();
foreach ($posts as $p) {
$post__in[] = intval($p);
}
$query->query_vars['post__in'] = $post__in;
return $query;
}
Then you would just access this:
http://mywebsite.com/?post__in=23,18,2,199,6,8
Please note that, like this, you won’t be able to set the post order in WordPress versions before 3.5 (#13729). Use this plugin if you need to.