Call to a member function have_posts() on a non-object on normal loop

Your code is being executed too early before the “base” query was evaluated.

As a thumb rule, in a plugin always hook your code to an appropriate hook, probably wp_loaded is best if there is nothing more specific