function enqueue_search_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('search-autocomplete', get_stylesheet_directory_uri() . '/js/search-autocomplete.js', array('jquery'), null, true);
wp_localize_script('search-autocomplete', 'ajaxurl', admin_url('admin-ajax.php'));
}
add_action('wp_enqueue_scripts', 'enqueue_search_scripts');
function fetch_mineral_suggestions() {
// Check for the 'query' POST variable to ensure it's set.
if (isset($_POST['query'])) {
$search_query = sanitize_text_field($_POST['query']);
// Define the query using WP_Query to fetch titles from the 'minerals' post type.
$args = array(
'post_type' => 'minerals',
's' => $search_query,
'posts_per_page' => -1 // Consider setting a reasonable limit here for performance.
);
$query = new WP_Query($args);
$results = [];
// Collect titles of posts that match the query.
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
array_push($results, get_the_title());
}
}
wp_send_json($results); // Send the results back as JSON.
}
wp_die(); // Terminate execution to return a proper response.
}
add_action('wp_ajax_fetch_mineral_suggestions', 'fetch_mineral_suggestions');
add_action('wp_ajax_nopriv_fetch_mineral_suggestions', 'fetch_mineral_suggestions');
quartz point medium pin Archives - Miami Mining Co - Gem Mining Buckets & Crystal Collections South FloridaIr al contenido