Allow multiple keyword search on WordPress, whilst using AJAX to return search results

I am using AJAX to load my search results on my site, but would like to extend the search to include multiple keywords. However, I cannot do so with a WP_Query (as far as I know).

This is the code I have currently in my search function:

function load_search_results() {
    $query = $_POST['query'];

    $i = 1;
    $args = array(
        'post_type' => 'days',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'order' => 'DESC',
        's' => $query
    );
    $search = new WP_Query( $args );

    ob_start();

    if ( $search->have_posts() ) : 

    ?>

        <?php
            while ( $search->have_posts() ) : $search->the_post();$count = $search->post_count;
                include(locate_template('day_format.php'));
            endwhile;

    else :
                include(locate_template('no_results.php'));
    endif;

    $content = ob_get_clean();

    echo $content;
    die();

}

I have tried adding the keywords as an array, but that doesn’t work. The normal search function parses the URL to get the keywords, but because of the AJAX loading I don’t think thats an option. I suspect I will have to use something other than WP_Query, but I am not sure what.

Any ideas?

Read more here: Allow multiple keyword search on WordPress, whilst using AJAX to return search results

Leave a Reply

Your email address will not be published. Required fields are marked *