1

I wanted to create a loop which displays search results in search.php file.

I followed all of the instructions in the WordPress Codex tutorial located in here:

https://codex.wordpress.org/The_Loop

I have implemented the suggested code on my page like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

The problem is that the else condition fires (displaying 'Sorry, no posts matched your criteria.') suggesting that the have_posts() function failed or returned no posts, but I am sure that what I am typing into the search box should return a lot of results.

IMPORTANT: I know that a lot of people in that situation suggest to use the WP_Query instance to get search results, and that does work, but the problem is I can't implement this suggestion. That's because I am also using a search enhancing plugin. It enhances relevance of search results and requires that no custom (WP_Query) calls are made, it allows only standard The Loop calls.

That is why I need to fix the problem with have_posts().

Could anybody suggest me what might be the cause of have_posts() execution failiure?

The full page code is below:

<?php

get_header(); 
    the_post(); 
?>

<?php define('WP_USE_THEMES', false); get_header(); ?>


    <div class="iecontent">
        <div class="g960">
            <div id="search">
                <div id="search-box">
                        <form action="<?php bloginfo( 'url' ); ?>" method="get">
                        <div>

                            <input type="text" name="s" value="<?php echo get_search_query()?>"  placeholder="Search..."/>
                            <input type="submit" name="searchsubmit" value="" class="submit"/>

                        </div>
                        </form>
                </div>
                 <div id="search-results">
                    <div class="search-result">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

                </div>
                </div>
                                <?php wpbeginner_numeric_posts_nav(); ?>
                <div class="clearfix"></div>
            </div>
        </div>
    </div>

<?php get_footer(); ?>
6
  • 1
    Why are you calling the_post outside the loop? And why do you display the header, then define use themes as false and display it a second time?
    – Tom J Nowell
    Commented May 9, 2014 at 12:23
  • These are left-overs from previous modifications. I think they do not have an influence on the result of that code.
    – NakedCat
    Commented May 9, 2014 at 12:27
  • Duplicate : stackoverflow.com/questions/23563782/…
    – Laxmana
    Commented May 9, 2014 at 12:39
  • Yes, I asked it here also to maybe get some attention from people specialized in WordPress development, this is not allowed? Because I am not sure.
    – NakedCat
    Commented May 9, 2014 at 12:43
  • 1
    You should not duplicate questions across the network.
    – s_ha_dum
    Commented May 9, 2014 at 13:59

2 Answers 2

2

Your problem is the weird code at the top of the page. To understand what's happening let me state a standard loop in pseudocode:

if we still have posts to process
    while we still have posts to process
        mark this post as processed and move to the next one
        do things with it ( like printing the title and contents

What you have is equivalant to:

mark the first post as processed
if we still have posts to process
    while we still have posts to process
        mark this post as processed and move to the next one
        do things with it ( like printing the title and contents

So if your search result only returns a single result, it will never be shown.

So this is what I recommend you do:

Remove the hocus pocus in your header

Anytime you see anything like this:

<?php

get_header(); 
    the_post(); 
?>

<?php define('WP_USE_THEMES', false); get_header(); ?>

I want alarm bells to start ringing. That code makes no sense, there's no reason to misuse WP_USE_THEMES like that, and it has no place in a theme. the_post should only be called inside a post loop, not outside, and calling get_header twice does no good. Call it once and remove the rest.

I have a strong feeling you do not understand what the code does or how it works. That's okay, nobody else does either, remove it. Anytime you find yourself resorting to such code, or flailing about with random things trying to get stuff to work, stop, and go researching the basics. It will save you time and clear your mind.

Indenting and php tag spam

You have so many potential issues that are easy to spot but hidden in difficult to read code

For example this is a bad sign and a waste of your time to type out:

?>

Each statement should be on its own line, and you should indent consistently. Your posts loop is a prime example of a lack of indenting. Your editor should automatically do this for you, but if it doesn't I recommend opting for something such as SublimeText or PHPStorm. There are many others out there.

get_template_part

Your misuse of get_header suggests you would benefit enormously from knowledge of how to use get_template_part.

0

I very much doubt that have_posts() is the point of failure. That function is very simple, basically just checking to see if $wp_query has post results that have not yet been displayed.

748   function have_posts() {
749           global $wp_query;
750   
751           return $wp_query->have_posts();
752   }

https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/query.php#L748

And...

3520          function have_posts() {
3521                  if ( $this->current_post + 1 < $this->post_count ) {
3522                          return true;
3523                  } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
                // truncated

https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/query.php#L3520

You are looking for the problem in the wrong place. The weird code at the top could be causing this, depending on what is hooked to get_header() but more likely the problem is your "search enhancing plugin". For it to work as you describe it must be altering the main query and my guess is that you are getting no search results as a result, instead of "enhanced relevance" as you expect. You can do two things to verify that:

  1. Disable the plugin and see if the default search mechanisms return anything
  2. add var_dump($wp_query->request,$wp_query->posts) to the top of the page so analyze the SQL and check the results manually, without have_posts() being involved.

Your Answer

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

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