Quantcast
Channel: Question and Answer » meta-query
Viewing all articles
Browse latest Browse all 36

get second ID from array

$
0
0

I need to get the second attachment ID from this function and put it into a separate custom field:

add_action('pmxi_saved_post', 'replace_thumbnail', 10, 3);

function replace_thumbnail($id) {
$image_ids = array();
$media = get_attached_media( 'image', $id );;
foreach($media as $item)
$image_ids[] = $item->ID;
$image_ids_str = implode(',',$image_ids);
update_post_meta( $id, '_easy_image_gallery', $image_ids_str );
}

Can anyone tell me how to do that?

I know how to change it to the custom field I want, just not how to single out the second or third ID.

I’ve tried putting numbers between the square brackets in the $image_ids, but that didn’t do anything. clueless where to start with this one.

I tried adding a stop on the third foreach loop.

add_action('pmxi_saved_post', 'replace_thumbnail', 10, 3);
function replace_thumbnail($id) {
$image_ids = array();
$media = get_attached_media( 'image', $id );
$i=0;
foreach($media as $item){
$i++;
if($i==3) break;
}
$image_ids[] = $item->ID;
$image_ids_str = ($image_ids);
update_post_meta( $id, 'test_id', $image_ids_str );
}

Viewing all articles
Browse latest Browse all 36

Trending Articles