I'm fairly new to PHP and I believe this is a fairly basic question. I've spent hours searching for an answer but can't quite seem to get things right. So any help would be appreciated.

I'm trying to create a gallery page on my site that automatically includes any newly uploaded images in it. My ultimate plan is using Justified Image Grid as my gallery manager. This requires putting the image ID in the JIG shortcode. In my template page I want to 1st - get all images IDs from the media library. 2nd - call the JIG shortcode and insert the IDs as the gallery images. From there I will set up filtering inside JIG.

From my understanding I can't get the image IDs directly from wordpress. I have to first get their URLs and then extract their IDs. If there is a more efficient way to do this please let me know.

So far I have this code to get all the image URLs (from this discussion: Get All Images in Media Gallery?):

$query_images_args = array(
    'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
);

$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
    $images[]= wp_get_attachment_url( $image->ID );
}

This works fine when I print the array.

Next I have this code to return the image ID from URL (From https://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/):

function pn_get_attachment_id_from_url( $attachment_url = '' ) {

global $wpdb;
$attachment_id = false;

// If there is no url, return.
if ( '' == $attachment_url )
    return;

// Get the upload directory paths
$upload_dir_paths = wp_upload_dir();

// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {

    // If this is the URL of an auto-generated thumbnail, get the URL of the original image
    $attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );

    // Remove the upload path base directory from the attachment URL
    $attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );

    // Finally, run a custom database query to get the attachment ID from the modified attachment URL
    $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );

}

return $attachment_id;
}

This works fine for one URL. What I can't figure out is how to loop through all the URLs returned by the $images array and return all the IDs. Can someone help or show me another way to tackle this problem?

up vote 2 down vote accepted

From my understanding I can't get the image IDs directly from wordpress. I have to first get their URLs and then extract their IDs. If there is a more efficient way to do this please let me know.

I'm not sure I follow you there, but did you try this:

$ids = get_posts( 
    array(
        'post_type'      => 'attachment', 
        'post_mime_type' => 'image', 
        'post_status'    => 'inherit', 
        'posts_per_page' => -1,
        'fields'         => 'ids',
    ) 
);
$images = array();
foreach ( $ids as $id )
    $images[]= $id;

print_r( $images );

where $images should contain the ID's of all the images.

For example:

Array
(
    [0] => 3299
    [1] => 3298
    [2] => 3297
    [3] => 3266
    [4] => 3265
    [5] => 3260
    [6] => 3259
    [7] => 3258
    [8] => 3257
)

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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