homegrade_theme_production/includes/api.php

46 lines
1.2 KiB
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 () {
register_rest_route('homegrade-datas/v1', '/questions-thematiques/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'get_questions_posts_per_thematique_id',
'args' => array(
'excluded_ids' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
));
});
function get_questions_posts_per_thematique_id($request)
{
$excluded_ids = $request->get_param('excluded_ids');
$excludedArray = explode(',', $excluded_ids);
$id = $request['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;
}