I'm trying to build a search that, depending on what user chosen in admin, will either query one of several external api search methods with user input, save all the results in database and then display them or it will search the custom tables in database, it should also be fully encapsulated in a plugin. I already have the business logic that does this, I'm having trouble hooking it up to WP. What would be the best way to accomplish this?

edit: sorry, it was probably quite vague question, what I have is an app I described above built on php framework, what I want to do is move it to WP, the problem is while the logic itself will require little modification, I have no idea how to hook it up to wordpress via plugin, essentially how do I create a form with plugin and how do I take input from that form and direct it to the logic that will handle that request?

  • can you clarify this phrase: "depending on what user chosen in admin" are you trying to ask "depending on which configuration options are chosen?"?
    – Will
    Dec 27, 2013 at 15:04

1 Answer 1


To change the search form, filter get_search_form. You get the form as a string here, and you can change it as you need.

add_filter( 'get_search_form', function( $form )
    // Replace the form, add additional fields
    return $form;

To change the search query to the database, filter posts_search. You get the query as a string and the current WP_Query object which provides more information. See wp-includes/query.php for context.

add_filter( 'posts_search', function( $search_query, $wp_query )
    // change the SQL
    return $search_query;
}, 10, 2 );

If you don’t want to filter the search query, change the name attribute in the search form, example foo, and inspect the $_POST request:

if ( ! empty ( $_POST['foo'] ) )
    $global $wpdb;
    $results = $wpdb->get_results( /* custom sql */ );
  • The problem is that besides editing the sql, I also need to hit an external api, saves results returned by api, fetch those results from db and then finnaly display them, it just doesn't seem like a good idea to put all that logic in a filter especially when I'm not even searching for posts but custom type.
    – Leylu
    Dec 28, 2013 at 20:16
  • @Leylu You can change the name attribute of the search form and hook in wp_loaded to perform your custom query when that name is set.
    – fuxia
    Dec 28, 2013 at 20:17
  • don't really understand, I overwriting form part, but how exactly would I get the search term when hooking into wp_loaded?
    – Leylu
    Dec 28, 2013 at 21:12
  • @Leylu See my update in the answer.
    – fuxia
    Dec 28, 2013 at 21:20
  • Thanks for your help, I've finally got it working by hooking into parse_request action and checking whether input I expect from my form exists in $_GET and if $_SERVER('request_uri') matches the pattern I want to catch, Would you say this is a good approach?
    – Leylu
    Dec 28, 2013 at 21:36

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.