WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Search only Pages? (6 posts)

  1. matthewordie
    Member
    Posted 3 years ago #

    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.

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    Yes - if you edit your theme's search.php template page.

  3. matthewordie
    Member
    Posted 3 years ago #

    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; ?>
  4. esmi
    Forum Moderator
    Posted 3 years ago #

    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; ?>
  5. matthewordie
    Member
    Posted 3 years ago #

    Perfect, thanks!

  6. esmi
    Forum Moderator
    Posted 3 years ago #

    No problem :-)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.