NakedCat
NakedCat

Reputation: 860

WordPress have_posts() failure on search page - searchWP plugin

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.

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?

UPDATE: As requested - I am adding the full page code 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(); ?>

Upvotes: 0

Views: 608

Answers (2)

Davit
Davit

Reputation: 594

You have called get_header() twice and have called the_post() after header please remove it.

Best Regards, Davit.

Upvotes: 1

Laxmana
Laxmana

Reputation: 1706

Based on the discussion we had on chat we came to conclusion that the plugin the OP uses (searchWP) alters the query and returns no posts.

When the OP disabled the plugin everything worked as expected.

Upvotes: 1

Related Questions