Codesnipp.it Social Code Sharing

Chris

Search just in posts OR just in pages

by Chris on Feb 13, 2011

// You want your WP search to only display results from posts? // or only from pages? It's simple :) // edit your theme's search.php // you will normally have something like this: &lt;?php if (have_posts()) : ?&gt; <h1>Search Results</h1><br/><br/> &lt;?php while (have_posts()) : the_post(); ?&gt; // TO SHOW ONLY posts // simply add this line: &lt;?php if (is_search() && ($post->post_type=='page')) continue; ?&gt; // So the final code it will be: &lt;?php if (have_posts()) : ?&gt; <h1>Search Results</h1><br/><br/> &lt;?php while (have_posts()) : the_post(); ?&gt; &lt;?php if (is_search() && ($post->post_type=='page')) continue;?&gt; // TO SHOW ONLY pages // just change == to != like this: &lt;?php if (is_search() && ($post->post_type!='page')) continue; ?&gt;

Can't see the comments? Please login first :)