mercredi 25 mars 2015

The writing ( with foreach) of the keys of an array of objects to a new array produces fatal error


I have the following problem, i've queried the attached media to a post with:



function abc_attachments( $post_id ) {
$query = array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image',
'numberposts' => '-1'
);
$images = get_children( $query );
return $images;
}


as a result i get an array of objects. But somehow i am unable to get the object keys transfered to a new separate array. The array of objects looks like that for example (i've left out the contained key value pairs):



Array
(
[1074] => WP_Post Object
(
)

[1067] => WP_Post Object
(
)
)


When i try to loop through the array of objects with foreach and write the object keys to an array i get



Catchable fatal error: Object of class WP_Post could not be converted to string


with the following snippet:



$media = abc_attachments( $post->ID );

foreach( $media as $key ) {
$arrayofids .= $key . ", ";
}
preprint($arrayofids);


Is there a more elegant and less fatal way?





Aucun commentaire:

Enregistrer un commentaire