Search code examples
wordpresswordpress-themingwordpress-shortcode

Wordpress auto commenter: Why are my automatically generated comments always marked as 'post-trashed'?


I set up a WordPress website, applied a theme, and added my own automatic comment function in the functions.php file of this theme. I want this function to automatically write a comment every time a question is posted. However, every time I try to publish a question, in the wp_posts table of the database, the comment_approved property of the automatically generated comment is automatically changed to 'post-trashed' for all posts with the post_type of 'question', even though I have set it to be 1. I don't know what to do next.In addition, I have not used any plugins on this website.

Here is my function in functions.php:

function ic_wp_insert_comment($postId) {
    $post = get_post($post_ID);
    $status = $post->post_status;
    $post_type = $post->post_type;
    #console_log($post_ID);
    #console_log($status);
    #console_log($post_type);
    if($status == 'publish')
    {
        #if($post_type == 'question')
        #{
        if (comments_open( $postId ) ) 
        {
            $data = array(
                'comment_post_ID'      => $postId,
                'comment_content'      => 'hello,my name is chatgpt. now i would like to introduce myself.',
                'user_id'              => 24,
                'comment_author'       => 'chatgpt',
                'comment_author_email' => '[email protected]',
                'comment_approved'     => 1
            );
            $comment_id = wp_insert_comment( $data );
            if ( ! is_wp_error( $comment_id ) ) {
                    echo 'auto comment error';
                return $comment_id;
            }
        
        }
        #}
    }
    return false;
}
add_action( 'wp_insert_post', 'ic_wp_insert_comment',99,1);

Here are the website and post: [enter image description here]() Here are the comments:(no qustion-post reply comments) [enter image description here]()


Solution

  • Comment that matched items in blocklist used to go either to moderation queue or to spam. NEVER to trash.

    There are two different lists in Settings -> Discussion:

    – Comment Moderation: any comment that matches any terms listed there would be set to “pending” in your comments section.

    – Disallowed Comment Keys: any comment that matches any terms listed there would be sent to the “trash” in your comments section.

    For “Disallowed Comment Keys”, it can also trigger on a partial “match”. For instance, if you had the term press listed in there, it would not only block “press” but other words that have that same character string: impressive, espresso, compression, depression, and so on.

    I would recommend auditing your “Disallowed Comment Keys” list to make sure you’re not blocking a general character string.

    If you see posts going to the trash again, compare their post to anything you’ve blocked in that “Disallowed Comment Keys” list to find why it was sent to the trash.

    I hope that clarifies things.