# Spam Comments Copied Across Several Posts

> Sends a comment to spam when exactly the same text was already posted on another post in the last day, a typical sign of a spam bot working through your site.

- Type: PHP
- Runs: Run everywhere
- Category: [Security](https://wpide.com/library/category/security/)
- Tags: comments, spam, duplicates
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/comment-duplicate-across-posts/

## Code

```php
add_filter( 'pre_comment_approved', function ( $approved, $commentdata ) {
    global $wpdb;

    if ( 'spam' === $approved || is_user_logged_in() || empty( $commentdata['comment_content'] ) || strlen( trim( $commentdata['comment_content'] ) ) < 30 ) {
        return $approved;
    }

    $found = $wpdb->get_var( $wpdb->prepare(
        "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_date_gmt > %s AND comment_post_ID <> %d AND comment_content = %s LIMIT 1",
        gmdate( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS ),
        (int) $commentdata['comment_post_ID'],
        $commentdata['comment_content']
    ) );

    return $found ? 'spam' : $approved;
}, 20, 2 );
```

## Notes

Short comments under 30 characters (like "Thanks!") are ignored so genuine readers are not caught.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Spam Comments Copied Across Several Posts" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
