The code automatically deletes WordPress posts that are older than 50 days just using the below SQL query.
add_action('wp', 'ts_delete_old_posts'); function ts_delete_old_posts() { global $wpdb; // Define the number of days after which posts should be deleted $days_old = 50; // Run the SQL query using $wpdb->query to delete posts older than X days $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE post_type = %s AND DATEDIFF(NOW(), post_date) > %d", 'post', // post_type to delete $days_old // Number of days old ) ); }