Support » Fixing WordPress » Search only Pages?

  • Resolved matthewordie

    (@matthewordie)


    Is it possible to search only pages and not posts? I use pages with custom fields for everything pretty much.

    I use posts for some random things and don’t want them appearing in my search without a page.

Viewing 5 replies - 1 through 5 (of 5 total)
  • esmi

    (@esmi)

    Forum Moderator

    Yes – if you edit your theme’s search.php template page.

    matthewordie

    (@matthewordie)

    Edit it in what way? Here’s what I’m using for my search code:

    <?php $posts=query_posts($query_string .
    '&posts_per_page=20'); ?>
    
    <?php if (have_posts()) : ?>
    <h1>Search Results</h1>
    <?php while (have_posts()) : the_post(); ?>
    
    <h1><a href="<?php the_permalink() ?>" rel="bookmark"
    title="Permanent Link to <?php the_title_attribute(); ?>">
    <?php the_title(); ?></a><br />
    </h1>
    
    <?php endwhile; ?>
    
    <?php endif; ?>
    esmi

    (@esmi)

    Forum Moderator

    Try:

    <?php $posts=query_posts($query_string . '&posts_per_page=20'); ?>
    <?php if (have_posts()) : ?>
    <h1>Search Results</h1>
    <?php while (have_posts()) : the_post();
    if( $post->post_type != 'page' ) continue;?>
    <h1><a href="<?php the_permalink() ?>" rel="bookmark"
    title="Permanent Link to <?php the_title_attribute(); ?>">
    <?php the_title(); ?></a><br /></h1>
    <?php endwhile; endif; ?>
    matthewordie

    (@matthewordie)

    Perfect, thanks!

    esmi

    (@esmi)

    Forum Moderator

    No problem 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Search only Pages?’ is closed to new replies.