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.

I don't think this is a parameter in get_comments( $args ); so I'm using a SQL statement. First I obtain a comment date from a separate table I created. Afterwards, I want to retrieve all comments made after that date.

$last_updated_comment = "
    SELECT comment_date FROM wp_last_comment
    ORDER BY comment_date DESC LIMIT 1";
$last_updated_comment = $wpdb->get_results( $wpdb->prepare($last_updated_comment));
print_r( $last_updated_comment );

This produces:

Array ( [0] => stdClass Object ( [comment_date] => 2012-03-09 09:03:00 ) ) 

Therefore, I add the foreach statement to access the object within the array:

foreach($last_updated_comment as $key => $object){
print_r( $object);
echo "<br /><br />";
}

@Stephen Harris, Thanks for the syntax help. Next, foreach object, get the date:

foreach($object as $key => $date_lookup){
    echo $date_lookup . '<br /><br />';
}

With output:

$date_lookup = 2012-03-09 09:03:00

Part 2: Comparing dates in SQL statement

Now that I've obtained the reference date, I want to get all comments from wp_comments after $date_lookup:

global $wpdb;
$sql = "
    SELECT comment_post_ID, comment_author, comment_author_email, comment_date, comment_content, comment_approved, comment_type, comment_parent
    FROM wp_comments WHERE comment_date > '%$date_lookup%' ";
$new_comments = $wpdb->get_results($sql);
print_r( $new_comments );

Now it seems that the > operator is looking at the time and ignoring the year because my output includes an array of comments with dates from 2011.

Is the > operator the correct tool to use?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

This a php syntax error, try:

foreach($last_updated_comment as $key => $last_comment_date){
    print_r( $last_comment_date );
    echo "<br /><br />";
}
share|improve this answer
    
AH, thanks Stephen –  AlxVallejo Mar 21 '12 at 14:50
    
@ Stephen, my problem seems to be unsolved. See part 2. For the sake of the community, I'd rather flush out the entire answer than just accept =>. Perhaps if you made it a comment, I could upvote it. –  AlxVallejo Mar 21 '12 at 15:32
    
AH, I solved it. See below for the answer –  AlxVallejo Mar 21 '12 at 15:48
add comment

$date_lookup must be wrapped in quotes (for a reason I cannot say with confidence).

The correct syntax for comparing the date:

global $wpdb;
$sql = "
    SELECT comment_post_ID, comment_author, comment_author_email, comment_date, comment_content, comment_approved, comment_type, comment_parent
    FROM wp_comments WHERE comment_date > '".$date_lookup."' ";
$new_comments = $wpdb->get_results($sql);
print_r( $new_comments );
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.