0

I am in need of putting an array of all images attached to a post into a custom field so that I can display they on the fly using my template's existing functions.

I have gotten this real close and am successfully able to set the value of the custom meta field _et_used_images with the contents of the array returned by get_children, I just need some assistance getting the values returned from get_children converted/sanitized to only include the post ID of the image and not the entire image object as I am getting now:

Here is the code I am using. I have tried using a foreach loop to add to a $get_children_array[] but I am missing something and just reverted back to where it was working, but returning ALL info instead of just postID. (Example of array returned at end).

            if ( 'thumbnail' == $column_name ) {
            // thumbnail of WP 2.9
            $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
            // image from gallery
            $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );

            if ($thumbnail_id)
                $thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
            elseif ($attachments) {
        //Set value of et_images via attached posts.


                foreach ( $attachments as $attachment_id => $attachment ) {
                    $thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
                }

            }
                if ( isset($thumb) && $thumb ) {
                    echo $thumb;
                //update_post_meta($post_id, '_thumbnail_id', $attachment_id); //Uncomment to generate thumbnails from attached media.
                update_post_meta($post_id, '_et_used_images', $attachments); 


                } else {
                    echo __('None');
                }
        }
}

EXAMPLE OF RETURNED ARRAY:

a:18:{i:897;O:7:"WP_Post":24:{s:2:"ID";i:897;s:11:"post_author";s:1:"3";s:9:"post_date";s:19:"2011-08-12 05:53:33";s:13:"post_date_gmt";s:19:"2011-08-12 05:53:33";s:12:"post_content";s:153:"A grand home located in Point Loma required a grand landscaping to complement the site. this was achieved with the site planting and landscape features. ";s:10:"post_title";s:33:"1-landscape-design-point-loma-job";s:12:"post_excerpt";s:0:"";s:11:"post_status";s:7:"inherit";s:14:"comment_status";s:4:"open";s:11:"ping_status";s:4:"open";s:13:"post_password";s:0:"";s:9:"post_name";s:35:"1-landscape-design-point-loma-job-3";s:7:"to_ping";s:0:"";s:6:"pinged";s:0:"";s:13:"post_modified";s:19:"2011-08-12 05:53:33";s:17:"post_modified_gmt";s:19:"2011-08-12 05:53:33";s:21:"post_content_filtered";s:0:"";s:11:"post_parent";i:713;s:4:"guid";s:95:"http://example.website.com/wp-content/uploads/2011/07/1-landscape-design-point-loma-job1.jpg";s:10:"menu_order";i:2;s:9:"post_type";s:10:"attachment";s:14:"post_mime_type";s:10:"image/jpeg";s:13:"comment_count";s:1:"0";s:6:"filter";s:3:"raw";}i:896;O:7:}}
5

3 Answers 3

0

The following code should work for you,

$images = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
$image_ids = array();
if($images){
   foreach( $images as $imageID => $imagePost ){
      array_push($image_ids, $imageID);
   }
}

The array $image_ids would get you the image IDs.

0

You can make use of the fields parameter with the value ids to get just those. You can use it with get_children(), which basically works like or is a wrapper for get_posts(), which again is wrapper for WP_Query. So you can do:

// this:
$children_images_ids = get_children(
    array(
        'post_parent'    => get_the_ID(),
        'post_status'    => 'inherit',
        'post_type'      => 'attachment',
        'post_mime_type' => 'image',
        'fields'         => 'ids'
    )
);

// or this:
$children_images_ids = get_posts(
    array(
        'post_parent'    => get_the_ID(),
        'post_status'    => 'inherit',
        'post_type'      => 'attachment',
        'post_mime_type' => 'image',
        'fields'         => 'ids'
    )
);

// or this:
$children_images_obj = new WP_Query(
    array(
        'post_parent'    => get_the_ID(),
        'post_status'    => 'inherit',
        'post_type'      => 'attachment',
        'post_mime_type' => 'image',
        'fields'         => 'ids'
    )
);
$children_images_ids = $children_images_obj->posts;

In any of those cases $children_images_ids will already be an array of IDs looking for example somewhat like this:

Array
(
    [0] => 123
    [1] => 456
    [2] => 789
)
0

OK, so I ended up going a completely different route with this and have a nice working solution

function update_images_meta( $object_id ) {
    // Get the post using its ID
    $post = get_post( $object_id );

    // determine if we are dealing with a post (a parent) or an attachment (a child)
    switch( $post->post_type ){
        case 'attachment':
            $post_id = $post->parent;
            break;

        default:
            $post_id = $post->ID;
            break;
    }

    if($post_id) {
        // Get all the image attachments for the post
        $param = array(
                'post_parent'       => $post_id, 
                'post_type'         => 'attachment', 
                'post_mime_type'    => 'image'
        );

        $attachments = get_children(  $param );

        // Initialize the array
        $atts = array();

        // Fill the array with attachment ID's
        foreach($attachments as $attachment)
            $atts[] = $attachment->ID;

        // Disable this hook which was overwrinting our changes
        remove_action( 'save_post', 'flexible_save_details', 10 );

        // Update the post's meta field with the attachment arrays
        update_post_meta( $post_id, '_et_used_images', $atts );

    }
}
1
  • Please accept your own answer. If you don't know how, visit the tour page and learn more how the site works. Also see help center :-) Oct 10, 2014 at 5:04

Your Answer

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

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