• Hi!

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

Viewing 15 replies - 1 through 15 (of 16 total)
  • 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.

    Thread Starter scooly

    (@scooly)

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

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

    Thread Starter scooly

    (@scooly)

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

    Any solution?

    Thread Starter scooly

    (@scooly)

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

    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.

    Thank you jsdalton! Quick and dirty worked for me.

    Thread Starter scooly

    (@scooly)

    Works for me too! Many thanks jsdalton.

    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,

    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

    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.

    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…

    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

    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

    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?

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Search only posts (not pages)’ is closed to new replies.