0

I am getting the following error when I try to query the WP database.

Call to a member function get_results() on a non-object in 'path here'

Here is the code I am trying to implement:

global $wpdb;
/* wpdb class should not be called directly.global $wpdb variable is an instantiation of the class already set up to talk to the WordPress database */
$result=$wpdb->get_results("SELECT * FROM wp_posts "); /* multiple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */
//echo "<pre>"; print_r($result); echo "</pre>";
/* If you require you may print and view the contents of $result object */
echo "ID" . "  " . "post_title" . "<br><br>";
foreach ($result as $row)
{
    echo $row->id . "  " . $row->name . "<br>";
}
/* Print the contents of $result looping through each row returned in the result */
benomatis
  • 5,536
  • 7
  • 36
  • 59
cathal
  • 15
  • 1
  • 7
  • 1
    Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – HamZa Mar 05 '16 at 18:40
  • Also take the time to format your question properly. – HamZa Mar 05 '16 at 18:41
  • 1
    your code looks okay. Are you sure you're calling it within WP scope where `$wpdb` is actually defined? – Igor Yavych Mar 05 '16 at 18:41
  • Thanks Igor - I believe the issue was that I was not referencing the config file: require_once('../../../wp-config.php'); – cathal Mar 06 '16 at 20:40

0 Answers0