• I am simply looking to exclude a few pages (not posts) from the search results on my site. I read the forums and found this solution below and placed this code in my search form php and updated the category id but it did not work. Is this the correct way to do that? I don’t want to install a plugin just to exclude a few pages from showing in the results. Thanks for any help in advance!

    <form…..>
    <input type=”hidden” name=”cat” value=”-your default category ID” />
    </form>

Viewing 11 replies - 1 through 11 (of 11 total)
  • No idea how that’s supposed to work. Try editing search.php and immediately after <?php while (have_posts()) : the_post(); ?>, add:

    <?php if( is_page(array(42,23,56) ) continue;?>

    where the numbers are the ids of the pages you want to exclude.

    Thread Starter WPML

    (@wpml)

    Esmi, you are always there to answer questions! Anyway, I just tried that exactly as you said and then tried a search that normally brought the page up in the results and got this error?

    Parse error: syntax error,………../public_html/wp-content/themes/Final Theme/Final1/search.php on line 49

    Whoops! My bad – I missed a closing ).
    Try <?php if( is_page(array(42,23,56) ) ) continue;?>

    Thread Starter WPML

    (@wpml)

    No problem, just did that on the searhc.php with the code below and the page with id #780 still shows up on the search. Does this look right?

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

    <div class=”art-Post”>

    Is 780 a page or a post?

    Thread Starter WPML

    (@wpml)

    It’s a page, I don’t have any posts right now. When I go and view all my pages this is what I see after the address when I hover over it so I’m assuming that’s the correct page ID #:

    https://……wp-admin/page.php?action=edit&post=780

    That sounds right. If you just want to exclude a single page, you can try simplifying that code down to <?php if( is_page('780') ) continue;?>

    Thread Starter WPML

    (@wpml)

    I’d like to exclude more then one page, but the code you recommended and that I tried above didn’t work. Any other suggestions would be appreciated.

    I was having the same issue and came up with this code. I looked around the web and found a few solution that where close but did not work in wordpress 2.9. This seems to be working great.

    Inside the search.php file change:

    <php? while (have_posts()) : the_post(); ?>

    to

    <php? while (have_posts()) : the_post(); if($post->ID == ‘PageYouWantSkipped’) continue; ?>

    If you want to skip multiple this should work:

    <php? while (have_posts()) : the_post(); if($post->ID == ‘00′ || $post->ID == ‘01′|| $post->ID == ‘02′) continue; ?>

    travis182

    (@travis182)

    What if I wanted to exclude a certain page from the search results, but also all of the future children without modding the search.php every time I add a childpage?

    Samuel Cook

    (@samuel-cook)

    I drove myself nuts trying to exclude individual pages from the search.php, while still keeping the original query and finally came up with this solution:

    placed in functions.php

    function filter_where($where = '') {
    	if ( is_search() ) {
    		$exclude = array(2480,2209,2454,2204,2488,2482,2463,2465,2458,2467,2461,2486,2211,2484,2200,2196,2198,2229,2219,2469,2490,2492,2218,2471,2473,2478,2496,2494,2476,2456);	
    
    		for($x=0;$x<count($exclude);$x++){
    		  $where .= " AND ID != ".$exclude[$x];
    		}
    	}
    	return $where;
    }
    add_filter('posts_where', 'filter_where');

    Where exclude array are the post ID’s.

    This will append to the original query_string and will still show the correct number of results, instead of being inside of the loop and excluding these pages, because when this happens, if I exclude say 8 of the 10 that will display I only see two results, and I need a constant 10. Hope this helps someone

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Exclude a few pages in search results?’ is closed to new replies.