How would I add custom tables/endpoints to the WP REST API?

I eventualy worked out a solution for my restaurants table, which sits alongside the wp_* tables in my WP database. Hope this helps

add_action( 'rest_api_init', function () {
  register_rest_route( 'restos/v1', '/all', array(
    'methods' => 'GET',
    'callback' => 'handle_get_all',
    'permission_callback' => function () {
      return current_user_can( 'edit_others_posts' );
    }
  ) );
} );

function handle_get_all( $data ) {
    global $wpdb;
    $query = "SELECT qname, rname, recommendation FROM `restaurants`";
    $list = $wpdb->get_results($query);
    return $list;
}

I have variations on this theme for all the CRUD actions I need

Leave a Comment