• Resolved dipokemon

    (@dipokemon)


    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)
  • Plugin Author Mikko Saari

    (@msaari)

    This is wrong: count($search_query). You can’t count $search_query, it’s a WP_Query object and not countable. If you want the number of results, use count($search_query->posts) or $search_query->found_posts.

    Does fixing that solve this problem?

    Thread Starter dipokemon

    (@dipokemon)

    Now the counter works as it should, it outputs 5. But I only added it to test it. The results still show only one record

    Thread Starter dipokemon

    (@dipokemon)

    UPD: Problem solved, I changed the use of the template.
    This line:

    load_template(get_template_directory() . '/template-parts/ajax-search-result-item.php');

    I replaced with this line:

    get_template_part('/template-parts/ajax-search-result-item');
    • This reply was modified 10 months, 2 weeks ago by dipokemon.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘I only get one result’ is closed to new replies.