WordPress : How to have query search posts only

March 8, 2012

If you need to have your WordPress search only search posts for results, you can insert this code before the ‘if(have_posts()):’ line on search.php and it will do that for you.

// search only posts
global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
query_posts( $args ); 

Further to that you can add any arguments from the standard WP_Query function to there to make the search more custom :)