For multiple search terms; ?s=hello+world

Wordpress work find "hello world" like the_title , the_content posts!

  • And, if our post title Hello Anna wordpress does not get one results!

I want to use all keys:

"hello world" , "hello" , "world"

Maybe array('hello world','hello','world'); but it exceeds my exp.! Is it in a single loop may be able to divide the query and send multiple queries? Is there someone who can help on the subject? e.g. ?s=, $_GET

Wanted something must have been like to call more results for multiple queries!

3 Answers 3


Fixed: Search and result the all keys;

        $the_keys = preg_split('/\s+/', str_replace('-',' ',get_query_var('s')),-1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
        $total_keys = count($the_keys);

        $the_query = new WP_Query(array('post_type'=>'nothing'));


            for($i = 0; $i<=$total_keys; $i++) {
                $the_query_mask = new WP_Query(array('s' => $the_keys[$i]));
                $the_query->post_count = count( $the_query->posts );
                $the_query->posts = array_merge( $the_query->posts, $the_query_mask->posts );

        } else {

            $the_query= new WP_Query(array('s' => get_query_var('s')));

        if ($the_query->have_posts()) : ?>

Note: 'post_type'=>'nothing' just need array merge!

  • it just needs to order the results by the default order after the array_merge
    – juz
    Commented Apr 15, 2016 at 1:57
  • Thank you. Yes that is right. It is also necessary to mask some symbols. e.g. How to ? need to remove ? key! But they're simple things. The hard part was solved. <3 wp Commented Apr 15, 2016 at 2:07
  • the other item that is missing is how the pagination would work (if you have any)
    – juz
    Commented Apr 15, 2016 at 2:28
  • Performance versus value is the question and test you should be running. This may work well on a simple site, but a blog that posts daily, it's a large query, and this would be quite taxing in many cases. A better solution might be to limit query variables in the UI to begin with? Commented Apr 15, 2016 at 2:41
  • My solution would be an Ajax type search, where you add filters to single variables. Commented Apr 15, 2016 at 2:42

Maybe somebody like me want fuzzy logic results

I solved this way: "search.php"

    $the_keys = preg_split('/\s+/', get_query_var('s'),-1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

    $total_keys = count($the_keys); // Count the search term

    $the_query = new WP_Query(array('s' => get_query_var('s')));

            // IF have multiple term and havent a result
            if($total_keys > 1 && $the_query->post_count < 1){

                // Loop for total term count, if found post break it! <3
                for($i = 0; $i < $total_keys; $i++) {

                    //Set the new array term value
                    $the_query = new WP_Query(array('s' => $the_keys[$i]));

                    //if found post break it! <3
                    if($the_query->post_count>0) break;


            // List of my Level 2 filter posts :)
            if ($the_query->have_posts()) : ?>



e.g. post title: ( Hello baby )


  • ?s=hello+baby => find true
  • ?s=hello => find true
  • ?s=baby => find true
  • ?s=hello+whats+up => find false
  • ?s=hey+baby => find false

search, Work now and find e.g. ?s=hello+whats+up => find true


You are getting into deep water quickly. I would look into SearchWP plugin as they provide LIKE searching and Fuzzy matching. SearchWP

  • 1
    Thank you @1Bladesforhire I know this logic, but exhaustive for servers! Plug-in create a term cloud. It not suitable for the size of the project. Commented Apr 14, 2016 at 21:24
  • I work on the $query = new WP_Query( array( 's' => 'keyword' ) ); 's' => array('keyword','keyword') but i need help. Commented Apr 14, 2016 at 21:47
  • @HybridWebDev no affiliation, just have used the plugin and liked it. Has the needed stuff and thought I would offer it as an alternative. Commented Oct 11, 2018 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.