The problem with your use of foreach
(in both cases) is that you are erasing any previous value $Comments
or $CommentDates
had before the current iteration. Rather than setting the value to the variable itself, perhaps you should be appending new values onto the variable as an array:
// Not sure what purpose your $cake and $element serve here )
foreach( $children as $cake => $element ) {
$args = array( 'post_id' => $ID ); // Assuming this $ID is declared elsewhere
$comments[] = get_comments( $args );
}
foreach( $comments as $commentObject ) {
$commentDates[] = $commentObject->comment_date;
}
Of course, these two foreach
statements could be blended into one single foreach
unless details not specified in your question would prevent that.
This would result in two arrays: $comments
containing all of the results from each iterative call to get_comments()
, and $commentsDates
containing the iterative results to each $commentObject
on its comment_date
property.