on Incentives REST API * Endpoint: /wp-json/incentives/v1/get-incentives */ /** * Register the custom REST route. */ add_action('rest_api_init', function () { register_rest_route('incentives/v1', '/get-incentives', [ 'methods' => WP_REST_Server::READABLE, 'callback' => 'pion_get_incentives', 'permission_callback' => '__return_true', ]); }); /** * Callback: return incentives grouped by province. * * @param WP_REST_Request $request * @return WP_REST_Response */ function pion_get_incentives(WP_REST_Request $request) { // Base response structure (index order can matter for your frontend) $response = [ 0 => [ 'province' => 'Federal', 'cards' => [], ], 1 => [ 'province' => 'Ontario', 'cards' => [], ], 2 => [ 'province' => 'Quebec', 'cards' => [], ], 3 => [ 'province' => 'Nova Scotia', 'cards' => [], ], 4 => [ 'province' => 'Prince Edward Island', 'cards' => [], ], 5 => [ 'province' => 'British Columbia', 'cards' => [], ], 6 => [ 'province' => 'New Brunswick', // 👈 NEW PROVINCE 'cards' => [], ], ]; // Pull incentives (adjust post_type if yours is different) $query_args = [ 'post_type' => 'incentive', 'posts_per_page' => -1, 'post_status' => 'publish', ]; $incentives = get_posts($query_args); foreach ($incentives as $incentive) { $title = $incentive->post_title; // Card payload – tweak fields if your frontend expects more/different keys $card = [ 'id' => $incentive->ID, 'title' => $title, 'content' => apply_filters('the_content', $incentive->post_content), 'link' => get_permalink($incentive->ID), ]; // Put the card into the right province bucket based on title text switch (true) { case stripos($title, 'Ontario') !== false: $response[1]['cards'][] = $card; break; case stripos($title, 'Quebec') !== false: $response[2]['cards'][] = $card; break; case stripos($title, 'Nova Scotia') !== false: $response[3]['cards'][] = $card; break; case stripos($title, 'PEI') !== false: case stripos($title, 'Prince Edward Island') !== false: $response[4]['cards'][] = $card; break; case stripos($title, 'British Columbia') !== false: $response[5]['cards'][] = $card; break; case stripos($title, 'New Brunswick') !== false: // 👈 NEW CASE $response[6]['cards'][] = $card; break; default: // Anything that doesn't match a province goes into Federal/other $response[0]['cards'][] = $card; break; } } return new WP_REST_Response($response, 200); } /** * CORS headers so your frontend can call the API from anywhere. */ function add_cors_headers() { header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: GET"); header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept, Origin, Authorization"); } add_action('rest_api_init', 'add_cors_headers', 15); n pion_get_incentives(WP_REST_Request $request) { // Base response structure $response = [ 0 => [ 'province' => 'Federal', 'cards' => [], ], 1 => [ 'province' => 'Ontario', 'cards' => [], ], 2 => [ 'province' => 'Quebec', 'cards' => [], ], 3 => [ 'province' => 'Nova Scotia', 'cards' => [], ], 4 => [ 'province' => 'Prince Edward Island', 'cards' => [], ], 5 => [ 'province' => 'British Columbia', 'cards' => [], ], 6 => [ 'province' => 'New Brunswick', // 👈 NEW PROVINCE 'cards' => [], ], ]; // Your existing query $query_args = [ 'post_type' => 'incentive', 'posts_per_page' => -1, 'post_status' => 'publish', ]; $incentives = get_posts($query_args); foreach ($incentives as $incentive) { $card = [ 'id' => $incentive->ID, 'title' => $incentive->post_title, 'content' => apply_filters('the_content', $incentive->post_content), 'link' => get_permalink($incentive->ID), ]; switch (true) { case strpos($incentive->post_title, 'Ontario') !== false: $response[1]['cards'][] = $card; break; case strpos($incentive->post_title, 'Quebec') !== false: $response[2]['cards'][] = $card; break; case strpos($incentive->post_title, 'Nova Scotia') !== false: $response[3]['cards'][] = $card; break; case strpos($incentive->post_title, 'PEI') !== false: case strpos($incentive->post_title, 'Prince Edward Island') !== false: $response[4]['cards'][] = $card; break; case strpos($incentive->post_title, 'British Columbia') !== false: $response[5]['cards'][] = $card; break; case strpos($incentive->post_title, 'New Brunswick') !== false: // 👈 NEW CASE $response[6]['cards'][] = $card; break; default: // no province match break; } } return new WP_REST_Response($response, 200); } // CORS function add_cors_headers() { header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: GET"); header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept, Origin, Authorization"); } add_action('rest_api_init', 'add_cors_headers', 15);