On my site, I want some pages to not be queriable by the search form (so they don’t appear when I’ve got something like www.ex.com/?s=banana)
Is there a way to “Remove” pages from the search results page (without just blindly do a condition of if is_page(id), display:none)
In WP_Query() there is a ‘post__not_in’ argument where you can exclude specific post ID’s.
You would create a new WP_Query inside of your search.php and use the current $query_args, then add on your ‘post__not_in’.
If you wanted to make it more dynamic, you could also build in some post meta where you could do a meta query and exclude all that have “exclude” checked. (look up ‘register_meta_box_cb’ in register_post_type ).
For example,
To exclude all pages from search results (i.e. without having to manually supply page IDs), here’s an improved version of @EricHolmes’s function:
More info:
(ALTERNATIVE) Although, given what you are doing, you could just set
exclude_from_search
for thepage
post type totrue
, which to me seems to be the best way to exclude a post type from search. See @prettyboymp’s answer for that.More info: