Title: query_posts with search
Last modified: August 19, 2016

---

# query_posts with search

 *  [paololazatin](https://wordpress.org/support/users/paololazatin/)
 * (@paololazatin)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/query_posts-with-search/)
 * 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)

 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/query_posts-with-search/#post-1328254)
 * 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](https://wordpress.org/support/users/paololazatin/)
 * (@paololazatin)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/query_posts-with-search/#post-1328366)
 * 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.

## Tags

 * [break](https://wordpress.org/support/topic-tag/break/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [paololazatin](https://wordpress.org/support/users/paololazatin/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/query_posts-with-search/#post-1328366)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
