I'm using the ZillaSortcodes plugin and add some functionality to build a shortcode to use the images uploaded to the post/page for an image grid.

In the shortcode.class.php of ZillaShortcodes I use this code to retrieve all images uploaded to the post

$referer = parse_url($_SERVER['HTTP_REFERER']);
preg_match('/post=(\d+)&/', $referer['query'], $referedID);
$actual_post = $referedID[1];

$attachments = get_posts( array(
    'post_parent'      => $actual_post,
    'post_type'        => 'attachment',
    'numberposts'      => -1,
    'post_status'      => 'any',
    'post_mime_type'   => 'image',
    'order'            => 'ASC',
    'orderby'          => 'menu_order',

To get the post ID I have to be a little bit tricky and getting the ID from the URL. This works fine if the post/page is already saved and the shortcode popup looks like this.

But this only works if the post is saved and there is a ID within the URL. Similar to the WordPress Media Manager where I can select "Uploaded to this post" I'm looking for a solution to get this to work even if this is a new post.

Thanks for any tips

1 Answer 1


The post object is already available when you are editing a post.

if you need it just call global $post;

There is also a handy little core function that does this for you, get_attached_media()

// Get the images uploaded to the post
$images = get_attached_media( 'image' ); 
  • Oh, sorry - I should have mentioned that I have tried calling global $post and with the function get_attached_media(). But the result is always an empty array.
    – Thomas
    Aug 23, 2013 at 11:10

Your Answer

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

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