<?php
if ( ! defined( 'ABSPATH' ) ) exit;
add_action('rest_api_init', function () {
register_rest_route('referenciak', '/v1', [
'methods' => 'GET',
'callback' => 'get_referenciak_all',
'permission_callback' => '__return_true',
]);
});
function get_referenciak_all(WP_REST_Request $request) {
$query = new WP_Query([
'post_type' => 'referenciak',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'no_found_rows' => true,
'update_post_meta_cache' => true,
'update_post_term_cache' => true,
]);
$references = [];
foreach ($query->posts as $post) {
$terms = get_the_terms($post->ID, 'referencia-kategoria');
if (is_wp_error($terms) || empty($terms)) {
$terms = [];
}
$references[] = [
'title' => get_the_title($post),
'featured_image' => get_the_post_thumbnail_url($post, 'full') ?: null,
'taxonomy' => array_values(array_map(function ($term) {
return [
'id' => (int) $term->term_id,
'name' => $term->name,
'slug' => $term->slug,
];
}, $terms)),
];
}
return rest_ensure_response($references);
}