I only get one result
-
Hello, I am creating a template and implementing a search based on the Relevansi plugin using Live Ajax Search, but have encountered a problem. For some reason only one record is displayed in the results.
Here is my PHP for the search:add_action('wp_ajax_nopriv_ajax_search', 'my_ajax_search'); add_action('wp_ajax_ajax_search', 'my_ajax_search'); function my_ajax_search() { global $wpdb; if (function_exists('relevanssi_do_query')) { $search_term = sanitize_text_field($_POST['search']); $search_query = new WP_Query(); $search_query->query_vars['s'] = $search_term; $search_query->query_vars['posts_per_page'] = 5; relevanssi_do_query($search_query); if ($search_query->have_posts()) { echo '<strong>' . count($search_query) . '</strong>'; while ($search_query->have_posts()) { $search_query->the_post(); load_template(get_template_directory() . '/template-parts/ajax-search-result-item.php'); } } else { echo 'Nothing.'; } } wp_die(); }
Here is my JS for the search:
function fetchSearchResults(searchValue) { var xhr = new XMLHttpRequest(); xhr.open('POST', '/wp-admin/admin-ajax.php', true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onload = function() { if (this.status === 200) { searchResults.innerHTML = this.responseText; handleSearchResults(); } else { searchResults.innerHTML = '<p>Search ERROR</p>'; showPopularQueries(); } }; xhr.onerror = function() { searchResults.innerHTML = '<p>Search ERROR</p>'; showPopularQueries(); }; xhr.send('action=ajax_search&search=' + encodeURIComponent(searchValue)); }
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘I only get one result’ is closed to new replies.