• Resolved Pete

    (@perthmetro)


    I have this function that deletes ALL comments after X days. How can I edit this so that it only applies to posts, not pages or attachment pages?

    function md_scheduled_delete_comments() {
    $comments = get_comments( array(
    'post_type' => 'post',
    'date_query' => array(
    'before' => '3 days ago',
    ),
    ) );
    
    if ( $comments ) {
    foreach ( $comments as $comment ) {
    wp_delete_comment( $comment, $force_delete = true );    
    }
    }
    }
    add_action( 'wp_scheduled_delete', 'md_scheduled_delete_comments' );
    // You can test it immediately by adding following line:
    // add_action( 'wp_loaded', 'md_scheduled_delete_comments' );
    • This topic was modified 5 years, 2 months ago by Pete.
Viewing 2 replies - 1 through 2 (of 2 total)
  • That code is returning only comments on posts for me when testing out on my local install. If you print_r($comments); into your theme’s index.php file for testing, does it list comments belonging to pages/attachments?

    Thread Starter Pete

    (@perthmetro)

    You’re absolutely right. I didn’t check first sorry. Thanks for clarifying.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Delete all post comments NOT attachment or page comments’ is closed to new replies.