Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8526845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:23:20+00:00 2026-06-11T08:23:20+00:00

I have an issue with not being able to call the get_results() function on

  • 0

I have an issue with not being able to call the get_results() function on the $wpdb object inside the WordPress functions.php file.

Exact error: Call to a member function get_results() on a non-object in […]

This is my function;

global $wpdb;
function query_students($year){
   $wpdb->get_results(
      $wpdb->prepare(
         "SELECT * FROM {$wpdb->prefix}usermeta WHERE meta_key ='foobar' AND meta_value = '{$year}'"
      )
   );
$wpdb->flush();
}

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.

Thanks in anticipation! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:23:21+00:00Added an answer on June 11, 2026 at 8:23 am

    “Globalizing” a variable that’s already in global scope does nothing. Case and point:

    global $a; //does nothing
    $a = 'foo';
    global $a; //does nothing
    foo();
    echo $a; //'foo'
    bar();
    echo $a; //'bar'
    function foo()
    {
        $a = 'bar';
    }
    function bar()
    {
        global $a;
        $a = 'bar';
    }
    

    The global keyword does not permanently make the defined variable global in scope. Think of it as a way to define a variable in a function and set its value to whatever a variable with the same name is outside of the function.

    You need to move your global declaration INTO the function to make the $wpdb object in the Global scope available within the function’s scope:

    function query_students($year){
        global $wpdb;
        $wpdb->get_results(
        $wpdb->prepare(
         "SELECT * FROM {$wpdb->prefix}usermeta WHERE meta_key ='foobar' AND meta_value = '{$year}'"
        ));
        $wpdb->flush();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a quick issue with my connection not being intialized. When I call
I have an interesting issue with HTTPS ports not being handled properly. It is
I have an issue which I could not find answer for across the web.
I have an issue and NO it is not homework, it's just a programmer
I have an issue in firefox and not in IE, didn't think I'd hear
So I have an issue that I'm not 100% sure on how to solve.
I'm not sure if I have a threading issue here or not. On the
I have seen a few posts regarding this issue but not one specific to
I have an issue with some HTML displayed in Drupal, but I am not
I have some urgent issue which I could not find answer for across the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.