Search page results conflicting logic – Search result caching?

Hm, I’m not sure, but your use of have_posts may be problematic.

Note the codex states “As a side effect, have_posts starts, steps through, or resets The Loop.”.

High up in your code you have

<?php if ( have_posts() ) : ?>
    <?php if ( have_posts() ) : the_post(); ?>

This may be causing unexpected results by messing with the loop.

EDIT

I think you’re getting ‘correct’ results for ‘Shortcodes’ (in your example) because ‘Shortcodes’ gets multiple hits. ‘Search’, however, has only one hit, and that hit is being looped over in your header, after which the loop is spent.

So, you need to reset the loop after running have_posts(), with
rewind_posts().

error code: 523