If I navigate to Posts -> All Posts in a Wordpress install, there's a Search Box in the upper right hand corner. Entering terms in this box and clicking the Search Posts button will perform a search.

Does Wordpress provide a hook, function, or other API that would allow me to programmatically perform the same (or a similar) search? i.e. I pass in a string as terms and get back a PHP array of results without needing to write any SQL myself.

If not, does anyone here know where in the Wordpress core code the above search happens? (so I cam mimic the behavior as best as possible)

Thank you!

  • 1
    try this : $posts = get_posts(["s" => "string to search"]);. you will find more informations in the codex : codex.wordpress.org/Function_Reference/get_posts
    – mmm
    Commented Aug 9, 2016 at 19:04
  • @mmm if that's an answer you should post it as one so it can be marked correct
    – Tom J Nowell
    Commented Aug 16, 2016 at 0:59

1 Answer 1


Per comments above from mmm

Try this

$posts = get_posts(["s" => "string to search"]); 

Also, Covering the get_posts API is a bit beyond a single StackExchange answer, but if you want to dive deep you'll find more informations in the codex.

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.