Wordpress Search Plugin Ajax Jun 2026
.search-results-list img width: 40px; height: 40px; object-fit: cover; margin-right: 10px; border-radius: 4px;
Add the following code to your theme's functions.php or your main plugin file:
$.each(posts, function(index, post) html += '<li>'; if (post.image) html += '<img src="' + post.image + '" alt="' + post.title + '">'; wordpress search plugin ajax
You need to place a search form in your theme (e.g., in header.php or a widget). The IDs here must match what is in your JavaScript.
because they use WP_Query or $wpdb->prepare() . However, cheap plugins that build raw SQL are dangerous. However, cheap plugins that build raw SQL are dangerous
// Set a small delay (300ms) before searching timer = setTimeout(function() $.ajax( url: ajax_search_obj.ajax_url, type: 'POST', data: action: 'ajax_search_action', // Must match the PHP hook suffix search_term: searchTerm, security: ajax_search_obj.nonce , beforeSend: function() resultsContainer.html('<p>Searching...</p>').show(); , success: function(response) if (response.success) var posts = response.data; var html = '<ul class="search-results-list">';
// Format and return JSON return array_map(function($post) return [ 'title' => get_the_title($post), 'url' => get_permalink($post), 'excerpt' => wp_trim_words($post->post_excerpt, 20) ]; , $query->posts); data: action: 'ajax_search_action'
.search-results-list li:last-child border-bottom: none;
| Approach | Example | Pros | Cons | |----------|---------|------|------| | | Default WP search + AJAX wrapper | No extra DB tables | Slow, no relevance tuning | | Indexed | SearchWP, Relevanssi | Fast, fuzzy matches, custom weights | Rebuilds index on save/update |
| Site Type | Recommended Plugin | Reason | |-----------|-------------------|--------| | Small blog (<1k posts) | Ivory Search (free) | Simple, zero config | | Medium blog / news | Relevanssi Premium | Best for text relevance | | WooCommerce store | Fibosearch (free or pro) | Blazing fast, respects stock | | Large custom site (20k+ posts, many fields) | SearchWP + Live Search | Indexes everything, highly tunable | | Budget, lots of custom post types | Ajax Search Pro (one-time $39) | One-time payment, many layouts |