Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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; }

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
This will throw an 'undefined variable' notice - the $commentDates array should be declared before the loop. –  Johannes Pille Nov 21 '11 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. –  Jonathan Sampson Nov 21 '11 at 0:49
    
Double brackets are key! –  AlxVallejo Nov 21 '11 at 18:11
add comment
$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:

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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