I've this error "Call to a member function get_results() on a non-object in" from this code:

require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-config.php');
require_once($_SERVER['DOCUMENT_ROOT'] .  $folder . '/wp-load.php');
if (!$wpdb) {
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
} else {
    global $wpdb;

function vendorDatails( $param)
    if ((substr ($param, 0, 0)) == '#')
        substr($param, 1);

    $order_ids = $wpdb->get_results( 
                                     SELECT DISTINCT vendor_id 
                                     FROM $wpdb->lqrhxf_pv_commission 
                                     WHERE order_id = %d

    foreach ( $order_ids as $order_id )
        echo $order_id->vendor_id;

The db parameters are loaded successfully but I've the error on the line above when I use $wpdb variable:

 $order_ids = $wpdb->get_results( 

2 Answers 2


This is a PHP variable scope issue, there is no $wpdb defined in your function.


global $wpdb;

before trying to use the $wpdb object.


Whenever you want to use WordPress' functionality outside of WordPress you can use

include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' ); Then you can use WordPress's functionality. Check This. If you want to just access the database functions then

require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' ); require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' ); if (!$wpdb) { $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST); } else { global $wpdb; }

  • Isn't the second one exactly what OP had already done? (except with an extra .$folder. in the path). By the way you can use the {} button in the editor to format lines as code (which is just indenting them by four spaces)
    – Rup
    Commented Jun 27, 2019 at 12:40

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.