[resolved] Dear god is there an easier way? to get the attachment ID? (4 posts)

  1. Zane Matthew
    Posted 5 years ago #

    Right next to my post I would like to have a "About this featured image section" which in short pulls in the data using
    wp_get_attachment_metadata its cake on a page like image.php where the attachment_id is in $_GET, But i'm trying to do this on my single-post.php page. Below is my working code:

    global $post;
            global $wpdb;
            $query = "
            SELECT ID
            FROM $wpdb->posts
            WHERE post_parent = '$post->ID'
            AND post_status = 'inherit'
            AND post_type='attachment'
            ORDER BY post_date DESC LIMIT 1";
            $attachment_id = $wpdb->get_var($query);
            $url = get_attachment_link($attachment_id);
            $urlArray = parse_url($url);
            $queryStringArray = explode('=', $urlArray['query']);
            $attachment_id = $queryStringArray[1];
            // assign some info
            $meta_array = wp_get_attachment_metadata($attachment_id);

    It works it gives me an array of attachment metadata, but I feel like i went the long way around the bush with this one. Any help on using some of the built in functions in wordpress to clean this up?

  2. Andrew Nacin
    Lead Developer
    Posted 5 years ago #

    For image.php or attachment.php, you can use $post->ID, $id, or I guess $wp_query->get_queried_object_id() would do as well.

    You want to avoid $_GET -- what if pretty permalinks are enabled? You'll also want to avoid direct queries if at all possible. A theme should never make a direct query, ever, because they should never need to.

    For single.php, you'll want to use get_children(), such as this (untested):

    $attachment = get_children(
       array( 'post_parent' => $post->ID, 'post_type' => 'attachment',
          'post_mime_type' => 'image', 'numberposts' => 1 ) );
    if ( count( $attachment ) )
       $metadata = wp_get_attachment_metadata( $attachment[0]->ID );


  3. Zane Matthew
    Posted 5 years ago #

    Thanks that did exactly what i wanted, except for one small issue. I've read about get_children, but over looked it cause I'm not intimidated about running queries, but given best practices, I'll stay away from them.

    The one issue i had was that I had to do an array_shift on the result from $attachment due to the result being something like this

        [351] => stdClass Object
                [ID] => 351
                [post_author] => 2
                [post_date] => 2010-09-05 22:12:34
                [post_date_gmt] => 2010-09-05 22:12:34
  4. Andrew Nacin
    Lead Developer
    Posted 5 years ago #

    It's not about queries being intimating, it's about them being un-cacheable. get_children() relies on the post caches. Direct queries are also not backwards compatible -- some people got caught by surprise when we added the 'trash' post status, for example.

    I didn't check to see how get_children() returned data before writing the snippet. It returns an array keyed by the post ID. So yes, you'll need to do an array_shift(). By default, it is an array of objects, so you'll then want $attachment->ID.

Topic Closed

This topic has been closed to new replies.

About this Topic