I'm writing a plugin to add a shortcut for displaying an image uploaded to your WordPress gallery on any post or page using a shortcode. I'm using the following statement to get attachments:
$attachments = get_posts('post_type=attachment');
However, this doesn't work reliably. It doesn't always include the specified attachment, even if the attachment exists in the wp_posts table. What would cause it to exclude some attachments?
Of course, I'd prefer a query that zeroed in on the specific attachment. Is there a WordPress function I can call to get the attachment I want based on ID or post_name? That would clearly be more performant than what I'm doing. I could go straight to SQL, but I'd prefer not to.
Thanks,
David