This is expected behavior and is enabled by default to enhance device security, as malicious chargers are how law enforcement and other actors have been able to gain access to iOS devices in the past:
Using USB accessories with iOS 11.4.1 and later - Apple Support
In short, to make sure your device charges:
1) Wake your iPhone
2) Connect the charger cable and wait for the charging indication
3) Your device will now charge
If it has been longer than an hour since you woke your phone, or more than three days since you charged your phone via a Lightning cable, it will not charge when you connect the cable.
Note wireless chargers will always work.
You can disable this device lockout by turning on Settings -> FaceID & Passcode (or TouchID & Passcode, whichever is applicable) -> Allow Access When Locked: USB Accessories.