How does the ‘the_post_navigation()’ work for CPT’s?

‘the_post_navigation’. It seems to work for Custom Post Types (CPTs) but I cannot figure out how it does this.

the_post_navigation() uses get_the_post_navigation() which uses get_previous_post_link() and get_next_post_link() which both uses get_adjacent_post_link() which uses get_adjacent_post() to get the next and previous post from the same post type.

If we look at the source code for get_adjacent_post(), we will notice that the $post_type property from the current post object ($post->post_type) is used as post type for the SQL query. This is how the links “know” how to get posts from the same post type. Here is the relevant line responsible for this

$where = apply_filters( 
    "get_{$adjacent}_post_where", 
    $wpdb->prepare( 
        "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type 
    ), 
    $in_same_term, 
    $excluded_terms, 
    $taxonomy, 
    $post 
);

I cannot figure out how to get the taxonomy of a Post on a single post page.

The taxonomy would be the the one which the terms are assigned to which is assigned to the posts. This is the custom taxonomy registered by you, or it might be the build in taxonomy category or post_tag. To learn what taxonomies and terms are, please refer to this post. The taxonomy name you have to hardcode, so you will need to know which taxonomy’s terms you need to target and loop through