first test on ajax
This commit is contained in:
@@ -83,4 +83,37 @@ class Partnerexpo_Core_Public {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function register_endpoint() {
|
||||||
|
register_rest_route('pexpo/v1', '/query', [
|
||||||
|
'methods' => 'GET',
|
||||||
|
'callback' => 'query_partners',
|
||||||
|
'permission_callback' => '__return_true',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function query_partners(WP_REST_Request $request) {
|
||||||
|
$args = [
|
||||||
|
'post_type' => 'pexpo_partners',
|
||||||
|
'posts_per_page' => 15,
|
||||||
|
'post_status' => 'publish',
|
||||||
|
];
|
||||||
|
|
||||||
|
$query = new WP_Query($args);
|
||||||
|
|
||||||
|
$posts = [];
|
||||||
|
|
||||||
|
foreach ($query->posts as $post) {
|
||||||
|
$posts[] = [
|
||||||
|
'id' => $post->ID,
|
||||||
|
'title' => $post->post_title,
|
||||||
|
'slug' => $post->post_name,
|
||||||
|
'url' => get_permalink($post),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_reset_postdata();
|
||||||
|
|
||||||
|
return rest_ensure_response($posts);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,14 +90,16 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// REPLACE THIS URL with your actual endpoint
|
// REPLACE THIS URL with your actual endpoint
|
||||||
// const response = await fetch(`/api/search?q=${encodeURIComponent(query)}&limit=${filters.maxResults}`);
|
const response = await fetch(`/pexpo/v1/query?q=${encodeURIComponent(query)}&limit=${filters.maxResults}`);
|
||||||
// const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
// --- SIMULATED FETCH FOR DEMO (Remove this block in production) ---
|
// --- SIMULATED FETCH FOR DEMO (Remove this block in production) ---
|
||||||
await new Promise(r => setTimeout(r, 600)); // Fake network delay
|
// await new Promise(r => setTimeout(r, 600)); // Fake network delay
|
||||||
const data = simulateBackendResponse(query);
|
// const data = simulateBackendResponse(query);
|
||||||
// ------------------------------------------------------------------
|
// ------------------------------------------------------------------
|
||||||
|
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
results = data;
|
results = data;
|
||||||
|
|
||||||
// 2. Compute local relevance if the API doesn't return it
|
// 2. Compute local relevance if the API doesn't return it
|
||||||
|
|||||||
Reference in New Issue
Block a user