I have created custom tables for performance reasons for products, carts, and notifications. I want to make these tables and their data available for normal CRUD via the WP REST API v2. How would I add custom tables (and their columns) to the API so that I could get/update those records?

3 s
3

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

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *