• Skip to main content
  • Skip to primary sidebar

Web Development Archive

  • Archive
You are here: Home / PHP / Register CPT endpoint

Register CPT endpoint

<?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);
}

Filed Under: PHP

About Gabor Flamich

I'm a web developer and designer based in Budapest, Hungary. In recent years, I've documented hundreds of solutions I came across during development. This site is an archive for useful code snippets on WordPress, Genesis Framework and WooCommerce. If You have any questions related to WordPress development, get in touch!

Primary Sidebar

  • angular.io
© 2026 WP Flames - All Right Reserved