Grow your CSS skills. Land your dream job.

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

  • # December 20, 2011 at 6:02 am

    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:



    < ?php query_posts( array( 'cat' => -65, 'paged' => get_query_var('paged') ) ); ?>
    < ?php if(have_posts()) : ?>< ?php while(have_posts()) : the_post(); ?>

    < ?php the_content(); ?>

    < ?php endwhile; ?>
    < ?php wp_reset_query();?>
    < ?php endif; ?>

    < ?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

    # December 20, 2011 at 12:16 pm

    Instead of using index.php, why don’t you create a new template called search.php?

    # December 21, 2011 at 2:34 am

    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

    # December 21, 2011 at 12:12 pm

    The search.php file will be your search results page. Here’s a loop for a search results page that I recently did:

    Search Results



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

    < ?php
    $searchquery = trim( get_search_query() );
    if ( !empty($searchquery) ) {
    printf( __( '

    Search Results for: %s', 'twentyeleven' ), '' . get_search_query() . '

    ' );
    } else {
    echo "

    Oops - you didn't specify a search term

    ";

    $searchquery = trim( get_search_query() ); echo "

    Please try searching again by entering one or two words on the subject that you are trying to find more information on.

    ";

    } ?>

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

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

    ">< ?php the_title(); ?>


    < ?php the_excerpt(); ?>

    < ?php } ?>

    < ?php endwhile; ?>

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

    < ?php else : ?>

    Nothing Found



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



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





    < ?php endif; ?>
    # December 27, 2011 at 2:23 pm

    Thanks Doc! :)

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".