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(); ?>
the_post
outside the loop? And why do you display the header, then define use themes as false and display it a second time?