How to Create Search in WordPress

[easy-social-share counters=0 style="button"]

I’ve expended lots of time finding out exactly why my search is not working for my custom made template. So far I was still able to discover how to include searchform.php file inside my header, set up search.php file folder which can be at this time blank (so that at this moment every time i do a search for some thing I instantly get redirected to a blank web page and also I do think I constantly need some thing in search.php file folder to make sure it is work), I was a lot reading through all over WordPress codex but yet can not try to find simple solution, only just useful guidance I find was this.

Is it possible to would suggest exactly what need’s to be achieved that allows you to show results of a search? will be there an unique query , function and etc ? I actually will not discover it is any where .

My searchform.php file folder just in case you require it.

<form action="<?php echo home_url(); ?>" id="search-form" method="get">

    <input type="text" name="s" id="s" value="type your search" onblur="if(this.value=='')this.value='type your search'"

    onfocus="if(this.value=='type your search')this.value=''" />

    <input type="hidden" value="submit" />

</form>

Simply, you are required to include the WordPress loop in your current search.php template to loop throughout the search results and also display all of them as a part of the template.

Below is a really simple example of this from The WordPress Themes Search Template and then Web page Template over at Theme Sharpe.

<?php

/**

 * The template for displaying Search Results pages.

 *

 * @package Shape

 * @since Shape 1.0

 */

get_header(); ?>

        <section id="primary" class="content-area">

            <div id="content" class="site-content" role="main">

            <?php if ( have_posts() ) : ?>

                <header class="page-header">

                    <h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?></h1>

                </header><!-- .page-header -->

                <?php shape_content_nav( 'nav-above' ); ?>

                <?php /* Start the Loop */ ?>

                <?php while ( have_posts() ) : the_post(); ?>

                    <?php get_template_part( 'content', 'search' ); ?>

                <?php endwhile; ?>

                <?php shape_content_nav( 'nav-below' ); ?>

          <?php else : ?>

                <?php get_template_part( 'no-results', 'search' ); ?>

           <?php endif; ?>

            </div><!-- #content .site-content -->

        </section><!-- #primary .content-area -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>

you will need to include the WordPress loop in your current search.php this is now example

search.php template file folder:

<?php get_header(); ?>

<?php

$s=get_search_query();

$args = array(

                's' =>$s

            );

    // The Query

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {

        _e("<h2 style='font-weight:bold;color:#000'>Search Results for: ".get_query_var('s')."</h2>");

        while ( $the_query->have_posts() ) {

           $the_query->the_post();

                 ?>

                    <li>

                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

                    </li>

                 <?php

        }

    }

else

{

?>

        <h2 style='font-weight:bold;color:#000'>Nothing Found</h2>

        <div class="alert alert-info">

          <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>

        </div>

<?php } ?>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

I am also making use of searchform.php and then search.php data files just as mentioned earlier, but then right here I provide you with the exact code.

Creating a Search Web page codex web page can help right here and also #Creating_a_Search_Page_Template displays the search query.

Inside my casing I pass the $search_query arguments and disagreements to the WP_Query Class (which often can determine whether is search query!). I after that run The Loop to show the post guides I wanted to, which in doing my case is the the_permalink and also the_title.

Search box form:

<form class="search" method="get" action="<?php echo home_url(); ?>" role="search">

  <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />

  <button type="submit" role="button" class="btn btn-default right"/><span class="glyphicon glyphicon-search white"></span></button>

</form>

search.php template file folder:

global $query_string;

$query_args = explode("&", $query_string);

$search_query = array();

foreach($query_args as $key => $string) {

  $query_split = explode("=", $string);

  $search_query[$query_split[0]] = urldecode($query_split[1]);

} // foreach

$the_query = new WP_Query($search_query);

if ( $the_query->have_posts() ) :

?>

<!-- the loop -->

<ul>   

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <li>

        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

    </li>  

<?php endwhile; ?>

</ul>

<!-- end of the loop -->

<?php wp_reset_postdata(); ?>

Hope you all like this Tuts byCodiTuts.com, if yes then keep us supporting by sharing this post, Cheerful Coding smiley.

Submit a comment

Pin It on Pinterest