When I filter media library to display only the images I want to include in a gallery (by a shortcode needing images id), it would be very useful if Wordpress showed somewhere a list of the ids of the displayed images. Currently I open images one by one to retrieve their id, and I feel this should not be the right way to do it.

Since I'm a Wordpress newbie, it might exist a different way to achieve the same result, such a plugin that I was not able to find, but anyway the functionality I described above (which I have not idea how to achieve) would solve the problem, and I think it would be very useful to many.

Thanks in advance.

Follow up

Building on Sunil Dora answer I wrote this snippet which allows to select images by one or more (by AND operator) category terms and use an existing shortcode to create a gallery (in the example below a Shortcode Ultimate plugin shortcode is created and executed, but you can tailor the snippet to build any kind of shortcode you want). I believe this functionality is very powerful because you don't have to modify your shortcodes to update your galleries, but you only have to assign the right category terms to attachments. For example if you want to add an image to a gallery, you don't have to edit the shortcode but just upload the new image and assign the right category terms to it. Here is the snippet, I inserted it at the end of my funtions.php, I hope someone is able to build a simple plugin with it and notify that here.

// usage: [mmj-taximids categoryname='media_category' categoryterms='term1,term2,term3']
// usage: [mmj-taximids size=120 categoryname='media_category' categoryterms='term1,term2,term3']
add_shortcode('mmj-taximids', 'mmj_taxonomy_image_ids');

function mmj_taxonomy_image_ids($atts)
    // Attributes
    $a = shortcode_atts(array(
        'categoryname' => 'not_provided',
        'categoryterms' => 'not_provided',
        'limit' => -1,
        'size' => 180
    ), $atts);

    // Output Code
    $tax_query_array = array();
    foreach (explode(',', $a['categoryterms']) as $value) {
            'taxonomy' => $a['categoryname'],
            'field' => 'slug',
            'terms' => array($value)
    if (count($a)>1) {$tax_query_array["relation"]="AND";}
    $args = array(
        'post_type' => 'attachment',
        'post_status' => 'any',
        'post_mime_type' => array( 'image/jpeg','image/gif','image/png','image/bmp','image/tiff','image/x-icon' ),
        'posts_per_page' => -1,
        'tax_query' => $tax_query_array
    $query_images = new WP_Query($args);
    $id_list = "";
    foreach ($query_images->posts as $image) {
        $id_list .= (($image->ID) . ",");
    return do_shortcode("[su_custom_gallery source=\"media: ".$id_list."\" limit=".$a['limit']." link=\"lightbox\" width=\"".$a['size']."\" height=\"".$a['size']."\"][/su_custom_gallery]");

2 Answers 2


Actually, wordpress stores the media library files also as a post format.

so you can follow this way to get your attachment media file and their id's.

Hope this will help you.

function get_images_from_media_library() {
$args = array(
    'post_type' => 'attachment',
    'post_mime_type' =>'image',
    'post_status' => 'inherit',
    'posts_per_page' => 5,
    'orderby' => 'rand'
$query_images = new WP_Query( $args );
$images = array();
foreach ( $query_images->posts as $image) {
    $images['id']= $image->ID;
    $images['src']= $image->guid;
return $images;

Next step is to create the HTML gallery or Your shortcode:

function display_images_from_media_library() {

$imgs = get_images_from_media_library();
$html = '<div id="media-gallery">';

foreach($imgs as $img) {

    $html .= '<img src="' . $img['src'] . '" alt="' . $img['id'] . '" />';


$html .= '</div>';

return $html;

You can use this code for your gallery shortcode.

  • Thanks, looks promising, but where should these snippets be put?
    – mmj
    Oct 9, 2017 at 9:13
  • 1
    you can add this snippet into your functions.php and create one shortcode so you may use that shortcode where you want to show your gallery content. Here is how can you create a shortcode link
    – Sunil Dora
    Oct 9, 2017 at 9:19
  • The first function get_images_from_media_library returns all the images in media library? If so, how can they be filtered by, for example, multiple categories of a custom taxonomy?
    – mmj
    Oct 9, 2017 at 10:24

You can get attachments just like any other type of post.

So, you can write WP_QUERY like this for applying the category filter to getting your media library images. Like this way,

$query_images_args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
'tax_query' => array(
        'taxonomy' => 'attachment_category',
        'field'    => 'slug',
        'terms'    => 'SLUG OF YOUR TERM',
$query_images = new WP_Query( $query_images_args );


Also, you can try like this,

$args = array(
'category' => $cat_ID or
'category_name' => $cat_name,
'post_type' => 'attachment'

$attachments = get_posts($args);

This query is same as well as how you write the WP_QUERY for getting the posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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