404 on wp-json url code example
Example: rest api response 404 wordpress
// if there is no posts return 404 with custom message
if( empty($query->posts) ){
return new WP_Error( 'no_posts', __('No post found'), array( 'status' => 404 ) ); // status can be changed to any number
}
// output
{
"code": "no_posts",
"message": "No post found",
"data": {
"status": 404
}
}