You don’t have to determine what type it is to query it, you just have to set the proper arguments to override defaults. First off, we’ll use WP_Query
to do additional queries instead of query_posts
.
If we only set the p
argument, post_type
defaults to post
, so we won’t get any pages:
$query = new WP_Query(
array(
'p' => 13
)
);
If we just add post_type
and set it to any
, we’ll get whatever post ID 13 is, whether it’s a post, page, attachment, etc..
$query = new WP_Query(
array(
'post_type' => 'any',
'p' => 13
)
);
If you have some post IDs and need to query all of them, you can get rid of the foreach loop and just do a single query for all the posts at once with the post__in
argument:
$query = new WP_Query(
array(
'post_type' => 'any',
'post__in' => array( 1, 2, 13, 42 )
)
);