5

I have a simple search form on my blog sidebar which will search only the blogs.

<form action="<?php echo get_site_url() ?>" method="GET">
   <input type="search" name="s" placeholder="Click to Search" class="fld-search" required/>
   <input type="hidden" name="post_type" value="post">
   <button class="btn-search"><i class="fa fa-search" aria-hidden="true"></i></button>
</form>

This is the website url : http://dev.wonder.lk/blog/

Following are the search types that I applied,

  1. Value that available in blog - For some values it gives the result, but sometimes it says not found. Ex : Search hello (but you can see the Hello World blog is there on the archive)
  2. Search someother post_type values - Result is a blank white page, even I can't see the header or footer. Ex : Search ninja It's a product type

These are the codes,

search.php

<?php
while ( have_posts() ) : the_post();
   if(isset($_GET['post_type'])) {
           $type = $_GET['post_type'];
           if($type == 'product') {
              get_template_part( 'woocommerce/archive', 'product' ); //working fine
           } else {
              get_template_part( 'framework/template-parts/page/search', 'post' );
           }
   } else {
           get_template_part( 'framework/template-parts/page/search', 'post' );
   }
   endwhile;
?>

search-post.php

<?php

get_header();

//Page Title Bar
$pageTitle = 'Search results for: "'.get_search_query().'"';
echo page_title_bar( $pageTitle, get_template_directory_uri().'/framework/assets/images/pg-title-bar.jpg');
?>

<div class="container blog-wrapper page-container">
    <div class="row">
        <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                <?php
                    // Include Blog Posts List
                    get_template_part('framework/template-parts/post/blog', 'post-list');
                ?>

            <?php endwhile; ?>

            <div class="pagination-wrapper">
                <?php pagination(); ?>
            </div>

            <?php else: ?>
                <h3>No results found for: '<?php echo get_search_query(); ?>'</h3>
            <?php endif; ?>
        </div>

        <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">
            <?php
                // Include Blog Sidebar
                get_template_part('framework/template-parts/post/blog', 'sidebar');
            ?>
        </div>
    </div>
</div>


<?php get_footer(); ?>

blog-sidebar and blog-post-list are the HTML structure.

Ask me if you want more details.

Ramesh
  • 2,297
  • 2
  • 20
  • 42
  • please put your code and with full code and files to that where your code are. – dev_ramiz_1707 Dec 21 '18 at 13:10
  • Hi why are you building a search form from scratch? You are using wordpress... – 13garth Dec 21 '18 at 13:27
  • @GarthBaker It's because I am building my own theme, and after seeing your comment I tried ` ` and It gave me the same result sir – Ramesh Dec 21 '18 at 13:49
  • @dev_ramiz_1707 please refer the question that I linked – Ramesh Dec 21 '18 at 13:50
  • are you try to search for " Thirundi Test " this word. because this work on you site too. – dev_ramiz_1707 Dec 22 '18 at 05:33
  • @dev_ramiz_1707 which search for that you have used? When you search the value `test` in the sidebar search field it direct to an white page? Header Search bar is working perfect since it is for `product_type` – Ramesh Dec 22 '18 at 07:43
  • Then you should add 404 page or use archive page when nothing found. M i right. – dev_ramiz_1707 Dec 22 '18 at 08:17
  • @dev_ramiz_1707 already have a 404 page, check this link http://dev.wonder.lk/dsadsa – Ramesh Dec 22 '18 at 08:25
  • hope this will help you. https://kinsta.com/blog/wordpress-white-screen-of-death/ – Priyank lohan Dec 24 '18 at 07:03
  • @Priyanklohan that article is mentioning about a common issue sir. In my case it happens only for few search values and it is stable, always it occurs when I search those values. – Ramesh Dec 24 '18 at 07:34
  • Is this question not valid for bounty? I am getting poor answers – Ramesh Dec 24 '18 at 08:29
  • Update the hidden field in your form code as follows. Add any other post type value if you have multiple post types and want those be included in result. – zipkundan Dec 24 '18 at 13:47
  • @zipkundan This is irrelevant to the question sir. I only search the `post`s in this search form – Ramesh Dec 25 '18 at 04:53

2 Answers2

2

It worked fine after updating the search.php as following,

<?php

get_header();

//Page Title Bar
$pageTitle = 'Search results for: "'.get_search_query().'"';
echo page_title_bar( $pageTitle, get_template_directory_uri().'/framework/assets/images/pg-title-bar.jpg');
?>

<div class="container blog-wrapper page-container">
    <div class="row">
        <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                <?php
                    // Include Blog Posts List
                    get_template_part('framework/template-parts/post/blog', 'post-list');
                ?>

            <?php endwhile; ?>

            <div class="pagination-wrapper">
                <?php pagination(); ?>
            </div>

            <?php else: ?>
                <h3>No results found for: '<?php echo get_search_query(); ?>'</h3>
            <?php endif; ?>
        </div>

        <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">
            <?php
                // Include Blog Sidebar
                get_template_part('framework/template-parts/post/blog', 'sidebar');
            ?>
        </div>
    </div>
</div>


<?php get_footer(); ?>

If I get a reasonable and well described answer I can offer the bounty for that answer.

Ramesh
  • 2,297
  • 2
  • 20
  • 42
0

Wordpress has a built in function to give you a search form

 <?php 
    get_search_form();
 ?>

https://developer.wordpress.org/reference/functions/get_search_form/

13garth
  • 655
  • 1
  • 8
  • 22