Filtering Search Results with WordPress

I’m trying to setup a Search Results page with two columns. First column will present results from all categories except one ( Galleries ), and the second column will present only the Galleries category.

query_posts() simply resets my results. This is what I got so far. Broken:

Read More
        <?php 
            $s = get_query_var('s');
            query_posts('s=' . $s . '&cat=164'); 
        ?>

        <?php 
            // First Loop 
        ?>
        <div class="contentLeft">
            <ul class="postListSmall related">
                <?php while (have_posts()) : the_post(); ?>
                    [do stuff]
                <?php endwhile; ?>

        <?php 
            // Second Loop
        ?>
            <?php query_posts('cat=-164'); ?>
            <?php rewind_posts(); ?>
                <?php while (have_posts()) : the_post(); ?>
                    [do stuff]
                <?php endwhile; ?>

    <?php else : ?>
                [do stuff]
    <?php endif; ?>

What to do?

Related posts

Leave a Reply

1 comment

  1. I know this is an old post but I am having a similar problem and thought I would share:

    1. You are creating a query, then calling a second query, but then trying to rewind the query. That’s not how the rewind function works. Take a look at the Rewind Documentation. You also say:

    query_posts() simply resets my results.

    Then why are you calling the rewind function immediately after the new query? Also, if you’re resetting the results then why is it a different query completely? This:

            $s = get_query_var('s');
            query_posts('s=' . $s . '&cat=164'); 
    

    Is not the same as this:

            <?php query_posts('cat=-164'); ?>
            <?php rewind_posts(); ?>
    

    To get 2 column results for different categories I did the following: use only one loop, don’t use rewind, use get_the_category in an if statement in your loop, for example:

    <?php 
    $s = get_query_var('s');
    query_posts('s=' . $s . '&cat=164'); 
    
    while (have_posts()) : the_post();
        foreach(get_the_category() as $category){
            if($category->name == "category name"){
                //Concatenate to the left div
            } else {
                //concatenate to the right div
            } ?>
    

    Hope this helps.