refactoring question data route to handle excludes in query parameter

This commit is contained in:
Antoine M 2023-10-25 14:41:34 +02:00
parent 0c40d820f0
commit d826a2ae8c

View File

@ -1,22 +1,44 @@
<?php <?php
/* --------------------------------------
SHIP QUESTIONS POSTS PER THEMATIQUE
--------------------------------------*/
// Handle exclusion parameters like http://homegrade.local/wp-json/homegrade-datas/v1/questions-thematiques/29?excluded_ids=1314,1305,1301
add_action('rest_api_init', function () { add_action('rest_api_init', function () {
register_rest_route('homegrade/v1', '/salut/(?P<id>\d+)', array( register_rest_route('homegrade-datas/v1', '/questions-thematiques/(?P<id>\d+)', array(
'methods' => 'GET', 'methods' => 'GET',
'callback' => 'my_awesome_func', 'callback' => 'get_questions_posts_per_thematique_id',
)); 'args' => array(
}); 'excluded_ids' => array(
add_action('rest_api_init', function () { 'sanitize_callback' => 'sanitize_text_field',
register_rest_route('homegrade/v1', '/salut', array( ),
'methods' => 'GET', ),
'callback' => 'my_awesome_func',
)); ));
}); });
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']; $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); $response->set_status(200);
return $response; return $response;