• Hi,

    My search is not displaying results for multiple word queries.

    For example if you type in one word from a title into search it’ll find the posts with that title but if you have a combination using that same word and another from the body text then I get page not found.

    Eg: search “dogs” works
    search “dogs everywhere” wont show any results.

    My search.php page is quite simple with a loop containing page title, excerpt and thumbnail if there is one

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter bradical911

    (@bradical911)

    if it helps get a few responses here is my code for search.php – nothing particularly exciting…

    <?php get_header(); ?>
    
    <section>
    
    <h1>Results for - "<?php the_search_query(); ?>"</h1>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <article id="post-<?php the_ID(); ?>">
    
    <h2 class="page-title" itemprop="headline"><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    <?php
    	if (has_post_thumbnail() ) { // show small pic
    		//echo "small pic";
    		echo "<div class=\"thumbnail-post thumbnail-small\">";
    		the_post_thumbnail( 'small' );
    		echo "</div>";
    	} else {}
    ?>
    
    <?php echo the_meta(); ?>
    
    <p>▸ <a href="<?php echo get_permalink(); ?>">Read More</a></p>
    
    <?php endwhile; else : ?>
    
    	<h1><?php _e( 'Page Not Found!'); ?></h1>
    
    </article>
    
    <?php endif; ?>
    
            </section>
    
            <aside>
    		<?php get_sidebar(); ?>
            </aside>
    
    <?php get_footer(); ?>
    Moderator bcworkz

    (@bcworkz)

    You need to be patient in these forums, it’s not a fully staffed help center, we are all just volunteering our time to support the WP community.

    Is this the default search box most themes place at the top of a page somewhere? This is not a sophisticated search interpreter like Google’s. It only works one way, a plugin is required to alter it to work differently. The search does not OR the search terms, they are ANDed. The more terms you provide, the fewer posts returned because only a few or none will have all the search terms.

    It sounds like you were expecting to get all posts that have either one, the other, or both terms, so more search terms yields more results instead of less. It just doesn’t work that way, sorry.

    FYI, the code you posted merely displays the search results, it doesn’t have anything to do with getting the results. The search parsing code is buried deeply in core code and few people know where to find it.

    Oh, and the search routine searches post content as well as titles, just FYI ??

    Thread Starter bradical911

    (@bradical911)

    Hi BCWorks,

    Thank for the reply, so its ANDing the search times, ok that makes sense.

    As I cant predict exactly what a user is going to type it only really works if it is doing “Ors” otherwise it fails as a search which makes it pretty useless especially when the words themselves are within the website maybe just not in the same order in the same place (could be anywhere in title, custom meta, content), just not that “exact phrase”.

    Do you know if there any plugins that get round this or can the search form itself be altered or functions.php have anything added.

    Funnily enough when I search for answers on this i don’t get much luck either.

    FYI my search form:

    <form id="search_form" action="https://www.champernowne.co.uk/beta/" method="get">
    
    			<input class="search" type="text" name="s" value="search..." onblur="if (this.value == '') {this.value = 'search...';}" onfocus="if (this.value == 'search...') {this.value = '';}" />
    
    			<input type="hidden" name="post_type" value="plant-catalogue" />
    
    			<input class="search-button" id="searchsubmit" type="submit" alt="Search" value="GO!" />
    
    		</form>
    Moderator bcworkz

    (@bcworkz)

    Yes, there are plugins that provide more sophisticated search capabilities. I suspect this is what the core developers intended – provide basic capability, then let the community develop more sophisticated capabilities as plugins. It’s impossible to anticipate everyone’s search needs, at least not without it turning into a major project of it’s own.

    If nothing else, you could add in one of those search by Google forms you often see on other sites. If you are capable of rolling your own code, you can hook the ‘posts_where’ filter and insert your own SQL WHERE clause when is_search() is true. The search terms should still be in `$_REQUEST[‘s’].

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search not displaying results for multiple word queries’ is closed to new replies.