WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

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' ); 
share|improve this answer
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 '13 at 11:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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