错误调用成员函数 get_results()

error Call to a member function get_results()

错误:

Call to a member function get_results() on null in C:\xampp\htdocs\shop\wp-content\plugins\myplugin\viwe.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\shop\wp-content\plugins\myplugin\viwe.php on line

代码

global $wpdb;

$query = $wpdb->get_results('SELECT * FROM test');

文件夹 myplugin 包含以下两个文件,

index.php

viwe.php

对于文件 viwe.php,代码无法正常工作并失败,

error Call to a member function get_results() on null in C:\xampp\htdocs\shop\wp-content\plugins\myplugin\viwe.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\shop\wp-content\plugins\myplugin\viwe.php on line

但是 index.php 正在工作。

在顶部添加这两行。

require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-config.php');
require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-load.php');

您需要包含这些文件,以便可以调用函数 get_results()