What is the best way to mimic a search query within a plugin

I’m in the process of building a WordPress plugin. I wish to mimic WP’s default search functionality but in the form of a ‘live search’.

I have my JS setup to grab the query string (i.e what would be /?s=search-term) & am passing it via AJAX to my function. But now I have to process the query to get the list of posts & pages. But I’ve read that it’s not right to simply use query_post from the WP documentation.

Read More

So my question is, what is the correct approach to yield the same search results as if the default search form was submitted?

Please can someone provide a code snippet or a link to a detailed article which clarifies this for me.

Thanks & please pardon my nativity!

Related posts

1 comment

  1. Pass the ‘s’ parameter to a new WP_Query object.

    $mys = new WP_Query(
      array(
        's' => 'whatever',
      )
    );
    

    That should duplicate the default search.

Comments are closed.