title: Wordpress - Fatal Error: "Call to a member function get_var() on a non-object" but only if not as Template
body: <p>I get "Fatal Error: "Call to a member function get_var() on a non-object"</p>
  
  <p>with this code in search.php:</p>
  
  <pre><code>global $owndb;
  $testtable = $owndb -&gt; prefix. "stuff";
  $values = $owndb-&gt;get_var( "SELECT COUNT(*) FROM $testtable" );
  </code></pre>
  
  <p>$owndb is created with <code>new wpdb</code> in my plugin which is active. search.php is located in the theme folder under "page-templates" but it is not defined as a template. The file is just called by a JavaScript file to send data. If I declare it as a template (add "Template Name" at the beginning) the error is gone. I tried to store the search.php in different locations (e.g. plugin directory) but this did not help.</p>
  
  <p>Why do I have to declare it as a template and how could I circumvent this?</p>
  
  <p>I saw several questions on the error but not with a solution that fits. Thanks!</p>