treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Wordpress Help: Search not working using a custom query on homepage

  • Hi guys,

    I have a site I'm currently working on but when I tried doing a search it didn't worked. Here's the code of from the index.php file:


    <!-- START OF POSTS -->
    <?php query_posts( array( 'cat' => -65, 'paged' => get_query_var('paged') ) ); ?>
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <div class="post">
    <?php the_content(); ?>
    </div>
    <?php endwhile; ?>
    <?php wp_reset_query();?>
    <?php endif; ?>
    <!-- END OF POSTS -->
    <?php wp_pagenavi(); ?>


    When I tried removing this line <?php query_posts( array( 'cat' => -65, 'paged' => get_query_var('paged') ) ); ?> my search worked but my pagination would be affected too.

    Is there anything I need to do? I hope someone can help

    Thanks guys! :)

    Val
  • Instead of using index.php, why don't you create a new template called search.php?
  • Hi Doc,

    My search area is located in the sidebar.php

    Can you tell me why I should create a search.php file?

    Thanks!

    Val
  • The search.php file will be your search results page. Here's a loop for a search results page that I recently did:
    <h1 id="headline">Search Results</h1>

    <?php if ( have_posts() ) : ?>

    <?php
    $searchquery = trim( get_search_query() );
    if ( !empty($searchquery) ) {
    printf( __( '<h2>Search Results for: %s', 'twentyeleven' ), '<span>' . get_search_query() . '</span></h2>' );
    } else {
    echo "<h2>Oops - you didn't specify a search term</h2>";

    $searchquery = trim( get_search_query() ); echo "<p>Please try searching again by entering one or two words on the subject that you are trying to find more information on.</p>";

    } ?>

    <?php /* Start the Loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>

    <?php $searchquery = trim( get_search_query() );
    if ( !empty($searchquery) ) { ?>
    <div class="search-result">
    <p><strong><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></strong></p>
    <?php the_excerpt(); ?>
    </div>
    <?php } ?>

    <?php endwhile; ?>

    <?php
    $searchquery = trim( get_search_query() );
    if ( !empty($searchquery) ) {
    wp_pagenavi();
    }
    ?>

    <?php else : ?>

    <h2>Nothing Found</h2>

    <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>

    <p>If you're still having trouble, try using some of the quicklinks below:</p>

    <ul>
    <li><a href="/programs/">Programs</a></li>
    <li><a href="/drop-in-schedules/rates-passes/">Rates &amp; Passes</a></li>
    <li><a href="/drop-in-schedules/by-activity/">Drop-in Schedules</a></li>
    <li><a href="/facilities-parks/recreation-centres/">Recreation Centre Hours</a></li>
    <li><a href="/contacts/">Contact Information</a></li>
    </ul>

    <?php endif; ?>
  • Thanks Doc! :)