I have a situation that didn't happen before. My search.php file didn't generate results, it just loop through all posts although the url is correct Domain_name/?s=key

Here is the searchform.php code:

<form action="<?php bloginfo('url'); ?>" id="search-form" method="get">
    <div class="form-group">
        <input type="text" name="s" id="search-control" class="form-control" placeholder="ادخل كلمات البحث هنا">
    <div class="form-group">
        <input type="submit" class="btn btn-default" value="ابدأ البحث">

Here is the search.php code:

<?php get_header(); ?>
    <section id="main" class="row">
        <div class="col-md-4" id="sidebar">
            <?php get_sidebar(); ?>
        <div class="col-md-8">
            <div id="blog-intro" >
                <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
                <?php if ( have_posts() ): ?>
            <h2>نتــائج البحث '<?php echo get_search_query(); /*global $wp_query; $wp_query->set('s',$_GET['s']); print_r($wp_query);*/ ?>'</h2>
            <div id="posts">
                <?php  while(have_posts()): the_post(); ?>
                    <div class="latestPostSingle row">
                        <div class="latestPostContent col-md-7">
                            <h5><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h5>
                            <div class="author"><p class="by"><?php the_author_posts_link(); ?></p></div>
                            <p><?php the_excerpt(); ?></p>
                        <div class="imageLatestPost col-md-5">
                                <img src="<?php echo IMAGES ?>logo_sm.png" />
                            <p class="post-info">
                                <span class="fa-comments"></span>&nbsp;<?php comments_number(); ?>&emsp;
                                <span class="fa-calendar"></span>&nbsp;
                                <span><?php the_time('j') ?></span>&nbsp;
                                <span><?php echo the_time('M') ?></span>
                                <span><?php the_time('Y') ?></span>
                <?php endwhile; ?>
                    <div id="paginate">
                        <?php my_pagination(); ?>
                <?php else: ?>
                    <h3>لا يوجد ما يطابق بحثك</h3>
                <?php endif;wp_reset_query(); ?>

<?php get_footer(); ?>

Note: If it changes anything, the website is in arabic.


Try adding the following code to the top of your search.php template below the get_header() call:

  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

  $search = new WP_Query($search_query);
  • thanks @kisabelle it worked :) . i also modified: if($search->have_posts()): while($search->have_posts(): $search->the_post(); but i want to understand why it didn't work at the beginning? – MEDZ Jul 10 '14 at 21:03
  • @GI_MED I'm not entirely sure and would have to look into it further. Wordpress documentation states: "Search results and Pagination may stop working when applying customization to the search template." It goes on to recommend adding that chunk of code to your Search template. Read more here: codex.wordpress.org/Creating_a_Search_Page – kisabelle Jul 10 '14 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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