8

I'm working on my own custom WordPress theme, using a blank, default template to start. I haven't edited the search.php file.

Many of my WordPress posts are Pages. Unfortunately, the site search only retrieves posts, not pages.

Any idea how to get the theme to search both posts and pages?

Here is the majority of search.php:

<?php if (have_posts()) : ?>

    <h3>Search Results</h3>

    <?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>

    <?php while (have_posts()) : the_post(); ?>

        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">

            <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>

            <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>

            <div class="entry">
                <?php the_excerpt(); ?>
            </div>

        </div>

    <?php endwhile; ?>

    <?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>

<?php else : ?>

    <h3>No posts found.</h3>

<?php endif; ?>
Ryan
  • 6,027
  • 16
  • 52
  • 89

4 Answers4

39

Add this code to your functions.php file.

function wpshock_search_filter( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array('post','page') );
    }
    return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');

http://wpth.net/limit-wordpress-search-results-to-specific-post-types

mark
  • 391
  • 3
  • 3
0

WP Search http://wpsear.ch/ has that ability.You can adjust what post types you want to show in results page.

0

In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>

Code to be inserted:

if (is_search() && ($post->post_type=='page')) continue; 

So, your code should be like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>

Let me know if it worked.

Rajeev
  • 1,376
  • 2
  • 15
  • 33
-1

Lack of page search and search results ranked by post date instead of relevance is a typical WP problem. Try http://wordpress.org/extend/plugins/relevanssi/

markratledge
  • 17,322
  • 12
  • 60
  • 106
  • That wasn't the question. It's not about relevance, it's about pages missing entirely. – Khom Nazid May 25 '19 at 00:30
  • why is this answer marked as correct? where are the "administrators" to check this. They only work when i spelled my sentences incorrectly malnacidos – Alejo_Blue Mar 29 '20 at 20:59
  • I don't think this answer should not be downvoted. It's not the correct answer as accepted, but the writer pointing out that the default WordPress search function sucks is fair. – Thomas Bennett May 12 '21 at 16:18