How to isolate code to the post edit screen

Use the WP_Screen object to tell where you are at in the admin instead. Much more convenient.

$screen = get_current_screen();
if ( $screen->id == 'edit-post' ) {
   // you're on the posts screen
}

Note that you have to wait until at least the admin_head hook to run for the current screen to have been determined and the WP_Screen object to therefore have been initialized. The current screen is not yet set at plugin load, or even at init.

You can use this to see the various properties that the screen object will have on various pages in the admin:

add_action('admin_head', 'show_screen_info_in_help_tab');
function show_screen_info_in_help_tab() {
    $screen = get_current_screen();
    $screen->add_help_tab( array(
        'id'      => 'screen',
        'title'   => 'Screen Info',
        'content' => '<pre>' . var_export($screen, true) . '</pre>',
    ) );
}

This will add a new section to the Help dropdown in WordPress, dumping the screen object into it as a “Screen Info” tab. Handy, and makes it easy to figure out the unique properties for each admin screen.