From d826a2ae8cffd277c90e93ebfe03b7dc46d45324 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Wed, 25 Oct 2023 14:41:34 +0200 Subject: [PATCH] refactoring question data route to handle excludes in query parameter --- includes/api.php | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/includes/api.php b/includes/api.php index 225d39c..583b4c4 100644 --- a/includes/api.php +++ b/includes/api.php @@ -1,22 +1,44 @@ \d+)', array( + register_rest_route('homegrade-datas/v1', '/questions-thematiques/(?P\d+)', array( 'methods' => 'GET', - 'callback' => 'my_awesome_func', - )); -}); -add_action('rest_api_init', function () { - register_rest_route('homegrade/v1', '/salut', array( - 'methods' => 'GET', - 'callback' => 'my_awesome_func', + 'callback' => 'get_questions_posts_per_thematique_id', + 'args' => array( + 'excluded_ids' => array( + 'sanitize_callback' => 'sanitize_text_field', + ), + ), )); }); -function my_awesome_func($request) +function get_questions_posts_per_thematique_id($request) { + $excluded_ids = $request->get_param('excluded_ids'); + $excludedArray = explode(',', $excluded_ids); + $id = $request['id']; - $response = new WP_REST_Response("salut" . $id); + $args = array( + // "exclude" => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), + "post_type" => "questions", + "posts_per_page" => -1, + "post__not_in" => $excludedArray, + 'tax_query' => array( + array( + 'taxonomy' => 'thematiques', + 'field' => 'term_id', + 'terms' => $id + ) + ) + ); + $postDatas = get_posts($args); + $response = new WP_REST_Response($postDatas); $response->set_status(200); return $response;