Forums

Search only posts (not pages) (16 posts)

  1. scooly
    Member
    Posted 7 months ago #

    Hi!

    I would like that WP search engine search only posts (now it search posts & pages) - anyone know how to do that?

  2. Gangleri
    Member
    Posted 7 months ago #

    That's funny, in older WPs we had to use plugins in order to make the searchfunction work for both posts and pages and now people want to exclude pages! I'm sure there's a plugin for you. Perhaps you only need to use Search Everything and uncheck pages, but I'm not sure. There's also an "Exclude Pages" plugin, but it doesn't mention the search function.

  3. scooly
    Member
    Posted 7 months ago #

    Unfortunately, those plugins doesn't work for me. Maybe some nice ninja-hack with query?

  4. Gangleri
    Member
    Posted 7 months ago #

    I'm sure that I saw a mention of an exclude pages search plugin somewhere in the forum, about three weeks ago.

  5. scooly
    Member
    Posted 7 months ago #

    I found that plugin but it doesn't work :/

    Any solution?

  6. scooly
    Member
    Posted 7 months ago #

    Hello? It is really so hard that no one know the solution?

  7. jsdalton
    Member
    Posted 6 months ago #

    This is not a good solution, but it's a quick and dirty way to do the job, along the lines of some of the code examples from the WP documentation on The Loop.

    So first, in the functions.php file in your theme, add this function:

    function is_type_page() { // Check if the current post is a page
    	global $post;
    
    	if ($post->post_type == 'page') {
    		return true;
    	} else {
    		return false;
    	}
    }

    Next, in your search.php file in your theme (where your search results are displayed, you want to add the following line right after the loop. It should look like this:

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

    That *will* exclude pages from your search results. (At least it does work for me, on WP 2.5.1.)

    The reason I dislike it is that it simply removes the pages from your result set. So let's say you have WP set to show 20 posts per page, but on a given search result set 7 of the results are pages. The result set displayed to the user will be 13 instead of 20! That's ugly.

    One thing you might consider to also prevent against this would be to give your pages a very early publish date, assuming you don't use the publish date anywhere. This would ensure that at a minimum they always show up at the end of your result set, which would ameliorate the problems caused by the above.

    If I hack together anything better than the above I will post it here.

  8. Atertract
    Member
    Posted 6 months ago #

    Thank you jsdalton! Quick and dirty worked for me.

  9. scooly
    Member
    Posted 6 months ago #

    Works for me too! Many thanks jsdalton.

  10. revlimiter
    Member
    Posted 3 months ago #

    hey jsdalton,
    Thanks for the advise! It works for me too (in WP 2.6)
    I am wondering if there is any known fix for that pagination issue...

    Cheers,

  11. martegale
    Member
    Posted 3 months ago #

    I just want to know the difference between writing a page and a post!

    I'm new to using Wordpress and haven't figured out the lingo, let alone where to go to get information.

    Anyone have the answer?

    Thanks in advance,
    Marte

  12. moshu
    Member
    Posted 3 months ago #

    where to go to get information.

    Start with your admin panel. Login and click Write.

    Secondly, start reading the Documentation. Scroll up and click on DOCS.

    Specifically, Pages.

  13. colino
    Member
    Posted 3 months ago #

    Yes, thanks jsdalton!
    Obviously, people who used to use the search everything plugin (and the likes) will be very happy with the new WP versions, but those who were happy with the older ones will now look for solutions not to search pages...

  14. jeeremie
    Member
    Posted 3 months ago #

    To remove pages from search, I add this code to functions.php:

    // Remove pages from search
    function mySearchPostsFilter($query)
    {
    if ($query->is_search)
    {
    $query->set('cat','12,14,16,17,18');
    }
    return $query;
    }
    add_filter('pre_get_posts','mySearchPostsFilter');

    That will include only your post categories in the search results, ('cat','12,14,16,17,18') being my category IDs. It works for me in WP 2.6.1

  15. Dgold
    Member
    Posted 3 months ago #

    that didn't seem to work for me jeeremie? Not sure what I'm doing wrong, I'll take a fresh look tomorrow at this.

    I added that code you put, in my theme's functions.php, after <?php

    and I changed the category numbers to '1,2,3,4'

    So far the search is working the same-as-it-was, including Pages alongside Posts in the search results. WP 2.6.1

  16. jeeremie
    Member
    Posted 3 months ago #

    Weird! It works very well for me. Maybe it is a problem with the code you wrote in search.php. Do you use the query_posts in search.php?

Reply

You must log in to post.

About this Topic

Tags