0

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?

2 Answers 2

0

This a php syntax error, try:

foreach($last_updated_comment as $key => $last_comment_date){
    print_r( $last_comment_date );
    echo "<br /><br />";
}
2
  • @ 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
    Commented Mar 21, 2012 at 15:32
  • AH, I solved it. See below for the answer
    – AlxVallejo
    Commented Mar 21, 2012 at 15:48
0

$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 );

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.