I have a search.php page in my template which works fine when used with the native search widget, but I am making a search plugin which has a custom wp_query in it and I want those results to appear on search.php INSTEAD of regular search widget results.

My plugin so far works well, I "just" want it outputted in search.php.

I tried adding pre_get_posts filter and also posts_search action but the filter hook shows "the connection was reset" in the browser and the action hook does the same PLUS it gives a WP Database error on the page where the plugin is loaded.

Relevant part of the function that makes the query:

$blog_url = get_bloginfo('url');

    $form = <<<EOH
    <div id="sbc">
    <form method="get" action="{$blog_url}" id="ss-search">
        <input type="text" value="{$search_text}" name="supers" onblur="if (this.value == '') { this.value = '{$search_text}';}" />
        <input type="submit" id="sbc-submit" value="Search" />

if (isset($_GET['supers'])) {

    global $q;
    // global $q;
    $args = array(
        // 'category__not_in' => 1,
        's' => $_GET['supers'],
        'paged' => get_query_var('paged')

    $q = new WP_Query($args);

    if ( $q->have_posts() ) {
            echo '<ul>';
        while ( $q->have_posts() ) {
            echo '<li>' . get_the_title() . '</li>';
            echo '</ul>';
            // echo '<br>Posts found :'.$q->found_posts.'<br><br>';
            echo get_pagination_links2();
    } else {
        echo 'no posts found';


    /* Restore original Post Data */

return $form;

} // end function
  • It's called via a shortcode.
    – dzimidula
    Nov 26, 2013 at 14:27

1 Answer 1


You are mixining the form display and the search logic. this is the first bad thing in your code.

Second, the hook you have used are triggered everytime a query run, even the query inside your function, this bring to an infinite loop.

So, first of all separate logic from display:

function vkss_super_search_form( $atts = '' ) {
  $atts = shortcode_atts( array(
    // your default params
  ), $atts );
  // some code you have here
  <div id="sbc">
  <form method="get" action="<?php echo $blog_url; ?>" id="ss-search">
  <input type="text" value="<?php $search_text  ?>" name="supers" onblur="if (this.value == '') { this.value = '<?php echo $search_text; ?>';}" />
  <?php echo $list; ?>
  <input type="submit" id="sbc-submit" value="Search" />
  return ob_get_clean();
add_shortcode('vkss_super_search', 'vkss_super_search_form');

Previous function print the form via shortcode.

Now you have to run the search when the form is submitted. You want to use the search.php but you have to notice that no one force to have that file on a theme, so you have to provide a fallback to index.php (just like WordPress does) if that file is not present.

You can use 'pre_get_posts', to check if the 'supers' GET variable is present and if so run change the main query. after that, the search.php will be used by WordPress because you are setting a search query.

add_action('pre_get_posts', 'vkss_super_search');

function vkss_super_search( $query ) {
  if ( ! is_admin() && $query->is_main_query() && $query->is_front_page() ) {
    $supers = filter_input( INPUT_GET, 'supers', FILTER_SANITIZE_STRING );
    if ( empty( $supers ) ) return;
    $query->is_search = true;
    $query->set( 's', $supers );
    // $query->set( 'category__not_in', 1 );

Note that all this second code block in unnecessary if you name your input field 's' instead of 'supers'.

<input type="text" value="{$search_text}" name="s" />
  • But where does my custom wp_query go? The custom query is very important because I'm gonna expand on it to include more things to filter once I get the basics set up. (sorry for the late reply, but this website is not functioning normally, less than 2 hours ago it said there were no replies). Also, it is no problem to use "s" if it will accept my custom filtering, I chose another name because I thought they'd clash.
    – dzimidula
    Nov 26, 2013 at 16:50
  • I added your last block of code. Now when I click "search" in my plugin, it DOES show the search.php page, but it uses everything from there still. I need it to use data from my custom wp_query. So for example, in my custom query (as you can see above), if there are no results it echoes "no posts found" , but in search.php it says "Your search for $var has returned $var results", and if I search for something that I know doesn't exist, I get the message from search.php and not my custom query.
    – dzimidula
    Nov 26, 2013 at 17:02
  • @dzimidula you don't need to create a custom WP_Query, you have to change the main query to include whatever you want. I've edited the answer adding the commented 'category__not_in' argument just like you do in question. You have to use that method to run the query that you need. You do not need to pass the 'paged' method because it is handled by WordPress by default on main query.
    – gmazzap
    Nov 26, 2013 at 17:24
  • I think it's working. Yes, I have to change the main query. I hardcoded some category ids to exclude now and used my plugin search button and it behaved the way I wanted it to! I could not have come to the solution on my own as I am quite new to WP. Thanks so much for taking the time!
    – dzimidula
    Nov 26, 2013 at 18:06

Your Answer

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

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