• I’ve been trying to make query_posts work with search but with no luck. Adding it displays all posts in the search results even those without matches. Can somebody point me to the right direction?

    <?php query_posts('author=1');  ?>
        <?php while ( have_posts() ) : the_post(); ?>
    	        <div class="posttitle"><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></div>
    			<div class="postbody"><?php the_excerpt(); wp_reset_query();?></div>
    
    	<?php endwhile;
    
    	//Reset Query
    	wp_reset_query();?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • By declaring the query_posts line as you’ve done above, you’re losing any queried variables.. in this case you’re losing the $s variable which holds the search text..

    Try the following:

    // Check the query variable is available
    if(!$wp_query) global $wp_query; // If not, global it so it can be read from
    // Your custom args
    $args = array( 'author' => 1 );
    // Merge the custom args with any for the query already
    $args = array_merge( $args , $wp_query->query );
    // Now do the query
    query_posts( $args );

    Thread Starter paololazatin

    (@paololazatin)

    Oh, that explains it, now I understand. This is what I ended up using:

    <?php query_posts($query_string . "&author=1");?>
         <?php while ( have_posts() ) : the_post(); ?>
              <!--post-->

    Many thanks, ma’am/sir! 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘query_posts with search’ is closed to new replies.