Please Note: 这样做会很慢!最好改为执行以下操作之一(或最好同时执行两者):
使用wp\\u cron定期执行此操作,而不是在完成后第一次在选项表中设置的时间戳运行时获取所有注释。每次之后,阅读时间戳并抓取自时间戳以来所做的评论。。。并在完成时更新时间戳
That being said:
要做到这一点,您应该在添加注释之前检查重复项,这需要确定什么是重复注释。
假设author\\u url和注释本身可以工作,您可以执行以下操作:
function my_comment_already_exists($author_url, $comment) {
$already_exists = false;
global $wpdb;
# try grabbing the first 40 characters of the comment. Hopefully that will make it unique
# also changed select from 1 as it_exists to comment_ID in case prepare broke that part
$comment_bit = substr($comment, 0, 40);
$like_bit = $wpdb->esc_like( $comment_bit ) . \'%\';
$query = "SELECT comment_ID FROM {$wpdb->prefix}comments WHERE comment_author_url = %s AND comment_content LIKE %s";
$query = $wpdb->prepare( $query, $author_url, $like_bit );
$results = $wpdb->get_results( $query );
if ( is_null($results) ) {
# error
if ( $wpdb->last_error ) {
echo "<p>Error: {$wpdb->last_error}</p>";
}
} else if ( is_array($results) && 0 < count($results) ) {
$already_exists = true;
}
return $already_exists;
}
然后,对于每个评论,您可以执行以下操作:
if ( !my_comment_already_exists( \'https://www.facebook.com/\'.$v->from->id, $v->message ) ) {
wp_insert_comment($data);
}