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
 }
}

Leave a Comment