如何通过 wordpress rest api 从自定义 table 获取所有记录?

How do I get all records from custom table via wordpress rest api?

我在 wordpress 数据库中创建了一个名为 custom_users 的自定义 table。我想通过我创建的 API 获取 custom_users table 中的所有记录。

functions.php

function get_wp_custom_users() {
  global $wpdb;
    $row = $wpdb->get_row("SELECT * FROM wp_custom_users");
    return $row;
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'wpcustomusers/v1', '/all/', array(
    methods' => 'GET',
    'callback' => 'get_wp_custom_users'
    ) );
} );

端点可以这样访问:http://localhost/mywebsite/wp-json/wpcustomusers/v1/all

当我通过 POSTMAN 访问端点时,我只看到 one record

您知道如何改进我的 get_wp_custom_users() 方法来检索所有记录吗?谢谢

您正在使用 get_row,它(顾名思义)获取一行。

要获得多行,我会改用 query

function get_wp_custom_users() {
  global $wpdb;
  $row = $wpdb->query("SELECT * FROM wp_custom_users");
  return $row;
}