wp query – sanitize_post() is not sanitizing Post Object

I wanted to sanitize a WP_Query post object without using foreach.

I used this method:

$args = array(
// ...
$data = array();

$the_query = new WP_Query($args);

$data("post") = $the_query->posts;

array_map("sanitize_post", $data("post"));

return rest_ensure_response($data);


enter image description here

as you can see that filter is working and its turning to “Display” from “Raw”.

But the problem is: <script> tags still standing in post_title field.

Whats the problem?