• Resolved mfal55

    (@mfal55)


    I’m wondering if anyone else has run into this. I have an issue with the built in search and query posts. On my index.php page, I have a query post above the -if has- posts line, which tells it to show all but category 3, like so:

    <?php query_posts( 'cat=-3' ); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
              ...... bunch o' stuff ......
    <?php endwhile; else: ?>
     <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>

    This works, however, when searching for a word that I know only appears in one post, instead of displaying that one post, the search results dump out every post (excluding category 3 items). If I search for a word that doesn’t exist, this works correctly, telling me that there are no results for that search term.

    If I remove the query post, the search works correctly, only showing the one post that uses that word.

    Does anyone know how to resolve this? Thanks in advance for any help you might have.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter mfal55

    (@mfal55)

    Anyone? 😀

    I forgot to mention, using wp 3.0.5.

    Try appending your custom query to the main query instead of replacing it.

    Thread Starter mfal55

    (@mfal55)

    Hi esmi! Thanks so much for your response. Sadly, I’m not sure what you mean exactly. Would you mind elaborating on this?

    <?php query_posts( 'cat=-3' ); ?> replaces the normal WordPress query – which in this case is your search query, So instead of running a search, WP return all posts except those in category 3. What you;re really after (I assume) is search results that include all relevant post except those in cat 3. so you don’t want to over-write the normal query. You just want to add some extra instructions – no cat 3 posts.

    Try replacing <?php query_posts( 'cat=-3' ); ?> with:

    <?php global $query_string; // grab the search query
    query_posts( $query_string . '&cat=-3' ); // add the extra details
    ?>

    http://codex.wordpress.org/Function_Reference/query_posts

    Thread Starter mfal55

    (@mfal55)

    YES!!! That worked! And now I understand what that means. I read that, but had no reference to what I’d be using that for in my particular case – but now I get it. Thanks so much for your help and patience – I truly appreciate it.

    Thread Starter mfal55

    (@mfal55)

    Hi again,
    I was wondering if you might help me flush out one more related thing. And again, I’m sure it’s something obvious, but I’m just not seeing it. Now the category I’ve excluded won’t show up in the search results. I saw on this page: http://codex.wordpress.org/Creating_a_Search_Page that I can preserve search results if I put this code at the top of the search template (which I’m assuming is my search.php page):

    <?php
    global $query_string;
    
    $query_args = explode("&", $query_string);
    $search_query = array();
    
    foreach($query_args as $key => $string) {
    	$query_split = explode("=", $string);
    	$search_query[$query_split[0]] = $query_split[1];
    } // foreach
    
    $search = new WP_Query($search_query);
    ?>

    I did that, but it doesn’t work. Any thoughts as to what I’m missing here? Again, thanks for your help – I hope this is the end.

    Thread Starter mfal55

    (@mfal55)

    I figured it out – sorry to have bothered you. For anyone interested, I put this code under the query_string and above the loop:

    <?php if (is_search()) { $posts=query_posts($query_string . '&posts_per_page=-1'); } ?>

    so the final code looks like:

    <?php global $query_string; // grab the search query
    query_posts( $query_string . "&cat=-3,-6" ); // don't show projects category
    ?>
    <?php
    	if (is_search()) { $posts=query_posts($query_string . '&posts_per_page=-1'); } //show all post results regardless of excluded category
    ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    Again, thanks for all your help!!

    No problem. Glad you got it working.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Query posts effecting search results’ is closed to new replies.