admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('template_conditions_nonce') ]); } } } public function abcbiz_select2_search_posts() { check_ajax_referer('template_conditions_nonce', 'nonce'); $searchTerm = sanitize_text_field($_POST['searchTerm']); $type = sanitize_text_field($_POST['type']); $results = []; switch ($type) { case 'post': case 'page': // Standard post types $query = new \WP_Query([ 'post_type' => $type, 'posts_per_page' => -1, 's' => $searchTerm, ]); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $results[] = ['id' => get_the_ID(), 'text' => get_the_title()]; } } break; case 'in_category': case 'in_post_tag': // Taxonomy terms $taxonomy = ($type === 'in_category') ? 'category' : 'post_tag'; $terms = get_terms([ 'taxonomy' => $taxonomy, 'hide_empty' => false, 'name__like' => $searchTerm ]); foreach ($terms as $term) { $results[] = ['id' => $term->term_id, 'text' => $term->name]; } break; case 'by_author': // Authors $users = get_users([ 'search' => '*' . esc_attr($searchTerm) . '*', 'search_columns' => ['display_name'], 'fields' => ['ID', 'display_name'] ]); foreach ($users as $user) { $results[] = ['id' => $user->ID, 'text' => $user->display_name]; } break; case 'not_found404': // Special case for 404 Page $results[] = ['id' => 404, 'text' => '404 Not Found Page']; break; default: $results[] = ['id' => 0, 'text' => 'No valid type provided']; break; } wp_reset_postdata(); wp_send_json_success($results); } }