I have a plugin folder foobar.

Inside this plugin I have a page called foobar.php. Inside this page I have

global $wpdb;
          $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'name'; //If no sort, default to title
          $sql = "SELECT * FROM wp_nc_location ORDER BY " . $orderby;
          $data = $wpdb->get_results($sql); 

This query works inside of my foobar.php page. However, in the same folder I have another page called process.php, and when I include the identical code:

global $wpdb;
          $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'name'; //If no sort, default to title
          $sql = "SELECT * FROM wp_nc_location ORDER BY " . $orderby;
          $data = $wpdb->get_results($sql); 

I get the error message:

Fatal error: Call to a member function get_results() on a non-object in...

Why doesn't the global $wpdb appear to work in the other page? (process.php)?

  • What exactly is a "page"? Is it a file? Or do you register it as page template? Further: Admin or public facing? Because the only thing I can say from your info is that $wpdb is not present, so maybe the whole wp env. is not present.
    – kaiser
    Aug 15, 2011 at 23:21
  • I mean a php page (file). Aug 15, 2011 at 23:24
  • Haha! What now: A file or a page? Page = something the user sees (post type: "page"). File = A real file that exists somewhere in a folder on a server and is not meant to be a "view". You could also tell how the file (if it is a file) gets loaded. This may be the problem causing the error.
    – kaiser
    Aug 15, 2011 at 23:28
  • No, I mean...I created an empty file, I called it process.php, and then I put in the code you see above. Otherwise do I have to call my sql using normal php code? Aug 15, 2011 at 23:31
  • Which I am suspecting...would be messy. Aug 15, 2011 at 23:34

1 Answer 1


The $wpdb object is part of WordPress, so wouldn't be loaded into a standalone PHP page as it is into a WordPress template.

You might want to look into creating your own page templates, then you could run your database query as part of that page template.

As a side note: You are currently trusting input from the user ($_REQUEST) which is a Bad Thing™, as it could make you vulnerable to SQL Injection attacks. The code example you give could be adapted as below to be less vulnerable:

global $wpdb;
$orderby = $_REQUEST['orderby'];
// Limit the values of orderby to ones we know to be safe
$acceptable_orderbys = array( 'name', 'age', 'height' );
if ( in_array( $orderby, $acceptable_orderbys ) )
    $clean_orderby = $orderby;
    $clean_orderby = 'name';
$sql = "SELECT * FROM wp_nc_location ORDER BY $clean_orderby";
$data = $wpdb->get_results($sql); 

I've not tested the above code, but the key thing is to check the value of $orderby is known to be safe before you put it into the DB query.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.