[wp-hackers] Newbie Q - can't get get_children to work

Jan Erik Moström lists at mostrom.pp.se
Sat Apr 11 18:19:31 GMT 2009


I'm in need of a sanity check here to see if I've misunderstood 
something. Thanks to the help of the mailing list I've cobbled 
together a plugin that does what I want it to do. If I enter 
something like

         [foto src='p1010146.jpg']

my plugin creates the markup I want, using the different image 
sizes (there are other options also but that's not important here).

So I set up my real site and uploaded a photo using my blog 
editor, inserted the markup in a new post and sent it to WP ... 
and it can't display the photos. The reason is that the photo 
need to be attached to a post and that only seem to happen if a 
URL to the image is in a post.

Since I used the following code

$images =& get_children( 
'post_parent={$post->ID}&post_type=attachment&post_mime_type=image' 
);

there was not any images found since there were no photos 
attached to this post.

As far as I can see there are two solutions to this:

+   Convert a relative path like "2009/04/p1010146.jpg' to the
     attachment ID so that I later can use the functions
     - wp_get_attachment_metadata
     - wp_get_attachment_image_src
     Unfortunately I don't know if there is a way to do this, I
     haven't been able to find any function that does this.

+   Change the parameters in the get_children call to
     'post_parent=NULL&...'
     since this lists all attachments. This works but I see one
     potential problem with this solution: if there are a large
     number of uploads the loop that tries to find the correct
     attachment will take some time (but I have no idea when
     this become a problem).

I don't really know how to handle this and would appreciate some 
advise. The current code looks like this:

$images =& get_children(
     
'post_parent={$post->ID}&post_type=attachment&post_mime_type=image' 
);
if( empty($images) ){
     $result = '<div class="errormsg">No photos attached to this post</div>';
} else {
     $result = '';
     foreach ( $images as $attachment_id => $attachment ) {
         $curphoto = wp_get_attachment_metadata( $attachment_id );
         $filename = basename($curphoto['file']);
         if( $photo == $filename ){
             return $this->format_this_photo(
                 wp_get_attachment_image_src( $attachment_id, 
$disp ),
                 wp_get_attachment_image_src( $attachment_id, 
$pop ));
         }
     }
     $result = '<div class="errormsg">That photo is not attached 
to this post :' . $photo . ':</div>';
}

(If the best solution is to use the NULL solution I should 
probably compare the whole path instead of just the actual filename)

Does anyone have any advise for how to handle this?

                     jem
-- 
Jan Erik Moström, http://mostrom.eu



More information about the wp-hackers mailing list