ylliX - Online Advertising Network

Quick tip – how to get specific posts/page by their ID?

At first look it was simple, just:

$args = array("post__in" => array(1,2,3));
$posts = new WP_Query();
$posts -> query($args);
while ($posts -> have_posts()){
$posts -> the_post(); 
	echo the_title().'<br/>';
}
wp_reset_postdata();

but, this way we can get ONLY posts, not pages. To get both posts AND pages just add:

'post_type'=>array('page', 'post')

in WP_Query arguments list so it will look like this:

$args = array("post__in" => array(1,2,3), 'post_type'=>array('page', 'post'));

And voila – we have both content types.

Leave a Reply