What’s an inclusive way of asking for a user’s gender and sexual orientation?

I need to ask for users' genders and sexual orientations in a normalized way that allows for filtering users on both properties (so I can't just store user-provider strings). What's the most inclusive way to do this?

For genders, ISO_5218 recommends having the options: 0 (not known), 1 (male), 2 (female) and 9 (not applicable); but this groups a lot of people who identify in many distinct ways in the same bucket. For sexual orientations I haven't found a standard.

Right now I'm thinking of having a set of genders and a set of sexual orientations that users can pick from, as well as having the option to provide a custom string for both. But that's not great for filtering (custom strings would not fall into any filters), and I don't know what genders and sexual orientations to include in the list.

Are there any inclusive established practices for this?