WordPress.org

Forums

[resolved] Possible? Search only posts, exclude pages (20 posts)

  1. Dave P.
    Member
    Posted 6 years ago #

    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.

  2. jidm
    Member
    Posted 6 years ago #

    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!

  3. konradhartmann
    Member
    Posted 6 years ago #

    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.

  4. Saurus
    Member
    Posted 6 years ago #

    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.

  5. jidm
    Member
    Posted 6 years ago #

    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.

  6. jidm
    Member
    Posted 6 years ago #

    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.

  7. renato_s
    Member
    Posted 6 years ago #

    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.

  8. renato_s
    Member
    Posted 6 years ago #

    Sorry! Use CONTINUE instead of BREAK!

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

  9. konradhartmann
    Member
    Posted 6 years ago #

    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]

  10. renato_s
    Member
    Posted 6 years ago #

    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.

  11. konradhartmann
    Member
    Posted 6 years ago #

    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**************

  12. konradhartmann
    Member
    Posted 6 years ago #

    Any ideas? (see previous post) Thanks

  13. renato_s
    Member
    Posted 6 years ago #

    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.

  14. konradhartmann
    Member
    Posted 6 years ago #

    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?

  15. konradhartmann
    Member
    Posted 6 years ago #

    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

  16. mewing22
    Member
    Posted 6 years ago #

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

  17. mikeque
    Member
    Posted 5 years ago #

    this works great, but now my search results pages are formatted as if the skipped pages are still there. For example I see a blank page and navigation to more results because the 5 items that should go on the first page were all pages, and thus filtered out.

    Can anyone point me to a way to better filter the formatting of the pages?

    Thanks

  18. Bull3t
    Member
    Posted 5 years ago #

    Bumping this because I also have the problem mikeque described. With this quick fix it messes up the whole search and pagination. Anyone got any ideas? Thanks in advance.

  19. samplesitedesign
    Member
    Posted 5 years ago #

    jidm,

    Brilliant. In query.php, I deleted the "any" and it resolved my issue. Not only were the pages showing up in results but the display was broken. All seems well now, although the site is brand new with little content yet, so I will hold my breath a while. Meanwhile, it looks good. Using WP 2.8.4 and Arras Theme 1.3.5. Just found this too - might be worth a look: http://wordpress.org/extend/plugins/search/

    Thanks!

  20. chrishajer
    Member
    Posted 5 years ago #

    Thank you renato_s. The $post->post_type=='page' bit was just what I was looking for to prevent my theme from displaying "Filed under:" and the default category on PAGE results shown in by search.php. I'm using the Search Everything plugin to include Pages in search results, but there's no concept of categories for pages. Your conditional takes care of this perfectly.

    Thank You.

Topic Closed

This topic has been closed to new replies.

About this Topic