• I am making a filter plugin and I want to use the search.php for the results, but if I set text input name to ‘s’ in my plugin, then it uses the regular search functionality, not the WP_Query method I set up in my plugin.

    And if I change the name and make the plugin use that name, I get the results but in the same place where the plugin itself appear.

    So, can I somehow make the search.php page use the results from my plugin?

    the form and my query:

    <?php
    
        $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="ss" onblur="if (this.value == '') { this.value = '{$search_text}';}" onfocus=if (this.value == '{$search_text}') { this.value = '';}" />
            {$list}
            <input type="submit" id="sbc-submit" value="Search" />
        </form>
        </div>
    EOH;
    
    if (isset($_GET['ss'])) {
            $args = array(
                // 'category__not_in' => 1,
                's' => $_GET['ss']
            );
    
            $q = new WP_Query($args);
    
            if ( $q->have_posts() ) {
                    echo '<ul>';
                while ( $q->have_posts() ) {
                    $q->the_post();
                    echo '<li>' . get_the_title() . '</li>';
                }
                    echo '</ul>';
                    echo get_pagination_links();
            } else {
                echo 'no posts found';
    
            }
            /* Restore original Post Data */
            wp_reset_postdata();
        }
    
        echo $form;
    
    ?>

    search.php

    <p class="search_results">Your search for <span class="searchterm"><?php the_search_query(); ?></span> has returned <?php echo $totalresults; ?> results.</p>
    
    <?php
    
    global $paged;
    if(empty($paged)) $paged = 1;
    
    $loop_counter = 1;
    
    $results_per_page = get_query_var('posts_per_page');
    
    echo '<ul class="search-list">';
    
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    
      if( $paged == 1 ) {
        $real_count = $loop_counter;
      } else {
        $real_count = $loop_counter + ( $paged * $results_per_page - $results_per_page);
      }
    
      echo '<li><span class="listnum">' . $real_count . '.</span>';
    
      // The Post
      ?>
        <a href="<?php the_permalink(); ?>"><?php echo substr(strip_tags($post->post_content), 0, 46);?>...</a> 
    
      <?php
    
      echo '</li>';
    
      // genesis_after_post();
    
      $loop_counter++;
    
      endwhile;
      // genesis_after_endwhile();
    
    else :
      // genesis_loop_else();
        echo '<p>No results found. Search for "page" for best results.';
    endif;
    
    echo '</ul>';
    
    ?>

    Also, pagination doesn’t work witht the custom WP_Query ($q). I DO get pagination links, but they don’t work. For example if I click on the second page, nothing happens. If I change $q to $wp_query it works, but then I can’t pass it the arguments I want.

  • The topic ‘Use search.php for custom search results? Also wp query pagination not working’ is closed to new replies.