title: Using $wpdb object in Wordpress functions.php file
body: <p>I have an issue with not being able to call the get_results() function on the $wpdb object inside the Wordpress functions.php file.</p>
  
  <p>Exact error: Call to a member function get_results() on a non-object in [...]</p>
  
  <p>This is my function;</p>
  
  <pre><code>global $wpdb;
  function query_students($year){
     $wpdb-&gt;get_results(
        $wpdb-&gt;prepare(
           "SELECT * FROM {$wpdb-&gt;prefix}usermeta WHERE meta_key ='foobar' AND meta_value = '{$year}'"
        )
     );
  $wpdb-&gt;flush();
  }
  </code></pre>
  
  <p>As you can see I've globalised the $wpdb variable, and this function works great in the page template file. I would just prefer it if my functions weren't dotted around the place, and in some kind of centralised file.</p>
  
  <p>Thanks in anticipation! :)</p>