0

This code works fine and returns two StdClass Objects for each of my two posts:

        $children = get_pages($args);

        foreach ($children as $cake=>$element) {
            $args = array(
                          'post_id' => $ID
                          );
            $Comments = get_comments( $args );
        };

This is great. I get each comment's properties but I am only interested in the comment date. My desired output is:

$CommentDates = (date1, date2, etc) -- Eventually I'd like to get the most recent comment date.

My next move is:

        foreach ($Comments as $CommentObject) {
            $CommentDates = $CommentObject->comment_date;
        }

This returns only one date and it happens to be the second object's value.

However, if I simply echo this foreach, I get both dates.

foreach ($Comments as $CommentObject) { echo $CommentObject->comment_date; }

2 Answers 2

1

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.

3
  • This will throw an 'undefined variable' notice - the $commentDates array should be declared before the loop. Commented Nov 21, 2011 at 0:43
  • However, if they're not, they will be created for you. Though for cleanliness, I agree that it is best to have them defined. That being said, I cannot commit myself to cleaning up the OP's code more than is necessary.
    – Sampson
    Commented Nov 21, 2011 at 0:49
  • Double brackets are key!
    – AlxVallejo
    Commented Nov 21, 2011 at 18:11
1
$CommentDates = array();
foreach ( $Comments as $Comment ) {
    $CommentDates[] = $Comment->comment_date;
}

will save the dates into an array.

$CommentDates = '';
$length = count( $Comments );
for ( $i = 0, $i < $length, $++ ) {
    $CommentDates .= $Comment->comment_date;
    if ( ! $i == ( $length - 1 ) ) {
        $CommentDates .= ', ';
    }
}

will create a string in the format you proposed in the question.

Resources:

Your Answer

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

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