Home >Backend Development >PHP Tutorial >Fatal error: Call to a member function get() on a non-object_PHP教程
Use require(BASE_PATH . ‘wp-load.php’); in the function of your own code to introduce WordPress core code, and then execute WP_Query to obtain specific logs, and then the following error occurs:
Fatal error: Call to a member function get() on a non-object in [path to site]\site\wp-includes\query.php on line 27
This is a problem caused by incorrect use of global variables. By default, $wp_query is defined in wp-setting.php. Because it is defined in the outermost layer, $wp_query is naturally a global variable. But here, the WordPress core code is introduced through require(BASE_PATH . 'wp-load.php'); in its own function, and $wp_query becomes a local variable in this function, so when other code calls $wp_query, it happens mistake.
The solution is: add the following code above the require(BASE_PATH . ‘wp-load.php’); code and define $wp_query as a global variable in advance:
define('WP_USE_THEMES', false); global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;