1// if there is no posts return 404 with custom message
2if( empty($query->posts) ){
3 return new WP_Error( 'no_posts', __('No post found'), array( 'status' => 404 ) ); // status can be changed to any number
4}
5
6// output
7{
8 "code": "no_posts",
9 "message": "No post found",
10 "data": {
11 "status": 404
12 }
13}