i am trying to import facebook comments to wordpress what iam doing is get the graph of the facebook post Comments, json decode it then i import the comment via this code
so my code so far is
foreach($comm_no as $answer_id => $v) {
$time = current_time('mysql');
$data = array(
'comment_post_ID' => $post->ID,
'comment_author' => $v->from->name,
'comment_author_email' => '',
'comment_author_url' => '/'.$v->from->id,
'comment_content' => $v->message,
'comment_type' => '',
'comment_parent' => 0,
'user_id' => 5,
'comment_author_IP' => '127.0.0.1',
'comment_agent' => 'egysp',
'comment_date' => $time,
'comment_approved' => 1,
);
wp_insert_comment($data);`
this code is in my single php
the proplem is every time the post is visisted the comments imported again an again an again so i have like 1000 comment of duplicate comments
can any one help me with this ?
i am trying to import facebook comments to wordpress what iam doing is get the graph of the facebook post Comments, json decode it then i import the comment via this code
so my code so far is
foreach($comm_no as $answer_id => $v) {
$time = current_time('mysql');
$data = array(
'comment_post_ID' => $post->ID,
'comment_author' => $v->from->name,
'comment_author_email' => '',
'comment_author_url' => 'https://www.facebook/'.$v->from->id,
'comment_content' => $v->message,
'comment_type' => '',
'comment_parent' => 0,
'user_id' => 5,
'comment_author_IP' => '127.0.0.1',
'comment_agent' => 'egysp',
'comment_date' => $time,
'comment_approved' => 1,
);
wp_insert_comment($data);`
this code is in my single php
the proplem is every time the post is visisted the comments imported again an again an again so i have like 1000 comment of duplicate comments
can any one help me with this ?
Please Note: Doing this will be very slow! It would be much better to do one (or preferably both) of the following instead:
That being said:
To do this, you should check for duplicates before adding the comment, which requires deciding what constitutes a duplicate comment.
Assuming the author_url and the comment itself would work, you could do something like the following:
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;
}
Then, for each comment, you could do something like:
if ( !my_comment_already_exists( 'https://www.facebook/'.$v->from->id, $v->message ) ) {
wp_insert_comment($data);
}