Support » Fixing WordPress » Possible? Search only posts, exclude pages

  • Resolved Dave P.

    (@callmedpit)


    I tried searching but found no definitive way to do this, but is it possible to limit blog searches to only posts? I do not want any pages included in the results that come up.

Viewing 15 replies - 1 through 15 (of 19 total)
  • I am looking for the same feature, and also found nothing. Please any info would be helpful. (even a hard-coding hack if no other way possible) Thanks!

    I was just about to submit this question. When people search for a movie title, I want them to get the appropriate post (review)–not the static page that lists all titles, the page for the Oscars, etc. If the post result were first, I could live with the other results–but it always shows up last.

    Thanks.

    It’s not an issue with me because I removed the search function from my blog. That said, I would imagine if it is possible to refine a search query – there would be a template tag for it in the Codex. You might want to check that area and see. If there is it would be a simple thing to insert the php into the main template page.

    I made it work by changing the file:
    wp-includes/query.php
    When it says: (line 836 on a wpmu 2.6)
    $q[‘post_type’] = ‘any’;
    It should say
    $q[‘post_type’] = ‘post’;

    Wondering if there is a way to do this by not editing this file. I mean by using a wp hack in the header, or in the search template. if anyone knows the proper way to do it, please tell me here.

    Tried with
    <?php $wp_query->set(‘post_type’, ‘post’); ?>
    Before have_posts(), tried it on header.php, but didn’t work either.
    (thanks to maymaym)

    Maybe there is something wrong (or to-do) on the code.
    Maybe some crew can check for it for future versions.

    ftr, im trying it on a wpmu 2.6 installation.

    In search.php, inside the Loop, you can test if it’s a page with an “if” statement and conditional tags. If it is, ignore it and proceed to the next result.

    Something like this:

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

    That should do it.

    Sorry! Use CONTINUE instead of BREAK!

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

    Thanks, but I am not clear where to put this code fragment. I am using a theme that uses CSS, and in the search form section of the theme editor, I was not able to insert this code without breaking search.

    Can you give some dummies-level instruction on exactly where and how to edit the correct file–either for renato’s suggestion or jidm’s?

    Thanks very much.

    Konrad

    [sig moderated]

    In search.php, inside the Loop

    If you don’t have a search.php file in your theme, you will have to edit the index.php. (according to the Template Hierarchy)

    Find The Loop and insert the code. You can recognize the Loop because it usually starts with:
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>

    Some themes can make it differently, though. If it doesn’t work, paste your index.php code here or in pastebin.

    Hey, guys. I don’t expect free programming, but if anyone is interested in taking a look, I have included the search.php and header.php. I have tried several times to find the right place to insert the fix to search only posts (not pages), but the result is either no visible change or some kind of fatal error message when visiting the site.

    I have some programming background so the concept is crystal clear. The problem is that I don’t know PHP syntax or CSS so unless the instructions are pretty specific to my situation, it is a little challenging. (Especially since there is no test environment.)

    Anyway, if someone wants to take a look and help, I will owe you big time. Since I have a page with an index to all movies, every search returns that page, which is ugly.

    Note: A fix that returned only a couple of lines from each page would also solve the problem. If you visit my site, you will see that any search on a movie name (e.g., Frozen River) fills all or most of the screen with the movie index, followed by other pages and finally the post, etc. It is really dumb for a search to return the index which is itself just another type of search! It would also be OK if the posts were always first, but that seems like an even more complex solution.

    Thanks very much, –Konrad

    **************Begin search.php**************
    <form method=”get” id=”searchform” action=”<?php bloginfo(‘home’); ?>/”>
    <div>
    <input type=”text” value=”<?php echo wp_specialchars($s, 1); ?>” name=”s” id=”s” />
    </div>
    </form>
    **************End search.php**************

    **************Begin header.php**************
    <?php get_header(); ?>

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

    <?php $post = $posts[0]; // Thanks Kubrick for this code ?>

    <?php if (is_category()) { ?>
    <div class=”page-title”>
    <h2 class=”page-title-border”><?php _e(”); ?> <?php echo

    single_cat_title(); ?></h2>
    </div>
    <?php } elseif (is_day()) { ?>
    <div class=”page-title”>
    <h2 class=”page-title-border”><?php _e(”); ?> <?php

    the_time(‘F j, Y’); ?></h2>
    </div>
    <?php } elseif (is_month()) { ?>
    <div class=”page-title”>
    <h2 class=”page-title-border”><?php _e(”); ?> <?php

    the_time(‘F, Y’); ?></h2>
    </div>
    <?php } elseif (is_year()) { ?>
    <div class=”page-title”>
    <h2 class=”page-title-border”><?php _e(”); ?> <?php

    the_time(‘Y’); ?></h2>
    </div>
    <?php } elseif (is_author()) { ?>
    <div class=”page-title”>
    <h2 class=”page-title-border”><?php _e(‘Author Archive’);

    ?></h2>
    </div>
    <?php } elseif (is_search()) { ?>
    <div class=”page-title”>
    <h2 class=”page-title-border”><?php _e(‘Search Results’);

    ?></h2>
    </div>
    <?php } ?>

    <?php while (have_posts()) : the_post(); ?>
    <div class=”post” id=”post-<?php the_ID(); ?>”>

    <div class=”title”>
    <h2 class=”posttitle”>

    rel=”bookmark” title=”<?php _e(‘Permalink to’); ?> <?php the_title(); ?>”><?php the_title();

    ?>
    </h2>

    <p class=”postmeta”>Reviewed on <?php

    the_time(‘M d, Y’) ?> – <?php the_category(‘, ‘) ?> <?php comments_popup_link(__(‘ ‘), __(‘1

    Comment’), __(‘% Comments’), ‘commentslink’, __(‘ ‘)); ?> <?php edit_post_link(__(‘Edit’), ‘

    · ‘, ”); ?>
    </p>
    </div>

    <div class=”postentry”>
    <?php if (is_search()) { ?>
    <?php the_excerpt() ?>
    <?php } else { ?>
    <?php the_content(__(‘Read the Rest of

    This Review »’)); ?><div style=”clear:both;”></div>
    <?php } ?>
    </div>

    <!–
    <?php trackback_rdf(); ?>
    –>

    </div>

    <?php endwhile; ?>

    <div class=”pages”>
    <div class=”pages-border”></div>
    <span class=”page-previous”><?php posts_nav_link(‘ ‘, ”,

    __(‘« Previous Reviews’)); ?></span>
    <span class=”page-next”><?php posts_nav_link(”, __(‘Newer

    Reviews »’), ”); ?></span>
    </div>

    <?php else : ?>

    <div class=”post”>
    <h2><?php _e(‘Error 404 – Not found’); ?></h2>
    </div>

    <?php endif; ?>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>

    **************End header.php**************

    Any ideas? (see previous post) Thanks

    Try this:

    In your INDEX.PHP file, 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();?>

    So, it 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.

    Hey, Renato. Yes, this worked, thanks! However:

    1. The formatting of the Search Results list is now a little goofed up…but it is functional. In any event, this is better than it was.
    2. Maybe (again), it is where I put it. I don’t have this line in index.php: <?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>

    I have <?php if ( have_posts() ) then a bunch of code and then while ( have_posts() ) : the_post();?>

    I put the code fragment on a line below this second piece: while ( have_posts() ) : the_post();?>

    Did I misplace it?

    Resolved by moving the line as shown below:
    <?php while (have_posts()) : the_post(); ?>
    <?php if (is_search() && ($post->post_type==’page’)) continue; ?>
    <div class=”post” id=”post-<?php the_ID(); ?>”>

    Thanks very much–and sorry for the huge posts on this topic. This is a great improvement, however, and I hope it benefits others as much as me.

    Regards, –Konrad

    mewing22

    (@mewing22)

    Thanks for this! It worked for me too. Now, any ideas on how to exclude pages in a “recent posts” widget?

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Possible? Search only posts, exclude pages’ is closed to new replies.