Incorrect search results in search.php – issue triggered by using `define(‘BodyID’, ‘search’); the_post();` in search.php

You should call the_post only inside the loop, or call rewind_posts() before actually starting the loop.

sidenote: you are doing it totally wrong. don’t reinvent the wheel, look in the codex how to write a theme or use one of the bundled default themes as a base to your theme. No sane person starts to code a theme from zero.