1

I would like to get 2 images in media by their IDs. If I want to get only one image I would use this code

$args = array(
    'p' => 1,
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'no_found_rows' => true,
    'posts_per_page' => 2,
    'post_status' => 'any'
    );

$query = new WP_Query($args);

How can I do it for 2 images, without double queries?

It would be great if I can pass an array with IDs but 'p' according to Codex can be only integer

    'p' => array(1,2),

p (int) - use post id. Default post type is post.

3

I think you need to use post__in . The code will be something like:

$args = array(
    'post__in' => array( 1, 2 ),
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'no_found_rows' => true,
    'posts_per_page' => 2,
    'post_status' => 'any'
);

$query = new WP_Query($args);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.