dynamisches suchfeld eingefügt - nocht

erstelle mit claude 4.5 - die auswahlfunktion funktioniert aber nicht das absenden nach dem auswählen in den beitrag hinein. -> "Block rendering failed"
This commit is contained in:
Jörg Lohrer 2025-10-01 13:25:30 +02:00
parent 9341afa1dc
commit 7437d10019
3 changed files with 161 additions and 26 deletions

View file

@ -147,4 +147,50 @@ function mbtnp_replace_tags( $content, $post_id = null ){
);
return $content;
}
}
// ---------------------------------------------------------------------------------
// ----- AJAX HANDLER FOR POST SEARCH ----------------------------------------------
// ---------------------------------------------------------------------------------
function mbtnp_search_posts_handler() {
// Check if user has capability to edit posts
if (!current_user_can('edit_posts')) {
wp_send_json_error(array('message' => 'Insufficient permissions'));
wp_die();
}
$search_term = isset($_POST['search_term']) ? sanitize_text_field($_POST['search_term']) : '';
if (empty($search_term) || strlen($search_term) < 2) {
wp_send_json_error(array('message' => 'Search term too short'));
wp_die();
}
// Search for posts
$args = array(
'post_type' => 'any',
'post_status' => 'publish',
's' => $search_term,
'posts_per_page' => 10,
'orderby' => 'relevance'
);
$posts = get_posts($args);
$results = array();
foreach ($posts as $post) {
$results[] = array(
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_date' => get_the_date('Y-m-d', $post->ID),
'post_type' => get_post_type($post->ID)
);
}
wp_send_json_success($results);
wp_die();
}
// Register AJAX handlers for logged-in users only
add_action('wp_ajax_mbtnp_search_posts', 'mbtnp_search_posts_handler');