You can use pre_get_posts action to exclude categories from search query.
function wcs_exclude_category_search( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( $query->is_search ) {
$query->set( 'cat', '-22, -21' );
}
}
add_action( 'pre_get_posts', 'wcs_exclude_category_search', 1 );
You should paste this code in your theme’s functions.php file.
Related Posts:
- How to query post like normal search would do. within search.php page
- Get all categories and posts in those categories
- WordPress Multiple Category Search
- Combine two taxonomies in a hierarchical tree
- How to get posts from two categories with WP_Query?
- Group WP_Query by category
- How can I reduce the number of database query calls for this custom homepage?
- WP_Query not looking at child category
- How to search for categories and/or tags?
- Get page content by category or tag
- Sticky posts on home page, search, tag and archives without plugin
- Create product category and keyword search form in woocommerce? [closed]
- Excluding posts from a category but only if they’re not in multiple categories
- What could be causing my wp_query pagination to break?
- Custom query with category exclusion and post-meta “whitelist”
- Exclude most recent posts from specific category in wp_query()
- Custom Query to search through categories
- How to show empty category in admin menus search
- Display one latest post from multiple categories
- How to Check if a Child Category is Being Queried
- How to select product category while adding new product in woocommerce? [closed]
- How do I get the total number of categories in a list of search results?
- Category Search functionality on category metabox (wordpress admin)
- Search by word, category, tag, author
- Filtering search results
- How to run WP_Query to retrieve attachments to posts only from a particular category?
- Searching through different categories on different pages code is not working
- How to query posts from specific authors and categories using WP_query?
- How to show in search results posts with a particular tag in a particular category?
- Help with multiple dropdown tags search
- Custom WP_Query for current category (in category.php)?
- List Posts by Category for a Non-Zero, Non-NULL Custom Field Value?
- getting content from main domain to sub-domain using category and WP_Query
- WP_Query() returns null when results exist!
- Multiple Category Search
- Ignoring a category in WP_Query still shows the links in next_post_link()?
- 3 Columns, 3 Categories, One Archive, and Pagination
- get_the_category and echo out link to child-most/deepest category
- I need to create a search form that will display search results from specific category
- Search Posts / Pages with multiple options?
- Enter a variable in the ‘category_name’ parameter
- WordPress search only showing a few posts
- Site search needs to find posts for category topic AND Tag or keyword thats not in the post
- Search in custom post type AND in pages with category
- Select posts by name and category per REST API
- Search Query for multiple categories using ‘OR’ but having certain categories be ‘AND’
- How do I toggle pagination on/off in search results and category listings via a link?
- Multiple Loops on category.php with pagination on last loop
- How to get latest posts from each category in regular order
- Refine/Filter the Search Results by Category
- change recent posts based on category
- How to remove specific categories from posts?
- Show matching categories in search page
- Keyword search in multiple categories
- Set colors depending on category
- WordPress Custom Search Form Displaying Unexpected Results
- Display posts from a category
- Custom loop of a single category, cannot order by date. What am I missing?
- Get URL Category Name By ID
- Pagination on category.php and tag.php not working
- category__not_in and id values from variable
- Use premade array as categories in wp_query?
- Hide products in uncategorized category from search results
- Group search results by category
- Woocommerce search form with category select
- Using if statement in index.php instead of creating separate template files
- WP_Query – using category__and when one of the categories is 0
- More efficient way to list posts by category [duplicate]
- Adding a block with query in homepage
- Query posts by views on category page
- I need to exclude from a query a category and a few custom taxonomies
- Show multiple categories in query using redux framework variable
- Are term_taxonomy_id and term_id the same? [duplicate]
- Return category posts with WP_Query
- Using in ‘category_name’ in ‘$query->set();’?
- multiple values in an array for category__and does not work with WP_Query
- Search form options group categories
- Custom Page Template Category loop not functioning correctly
- Retrieve post tags from from main wp_query
- Posts of specific category on page and excluded from index.htm
- new WP_Query issues
- How can I use AJAX with check-box categories?
- Select posts that match multiple category names, must match all categories listed
- the_category() doesn’t working in wp_query loop
- How to display the 5 latest post titles but allow only 1 in there of a specific category
- Show Posts via cat+cat in URL that are in both Categories doesn’t work
- WordPress Default Category and Custom Taxonomy Selected Attribute not Working After Searched in wp_dropdown_categories Array
- Automatically adding tags and categories into Post Excerpt for searching
- Return all Tags and Categories in Separate List
- Categories In English version showing not canonical URL, instead shows query search result
- Search Replace Database ONLY for posts of certain category?
- Search by tag, category and author without plugin
- Issue with WordPress category search
- get_terms sort order with child categories of varying depth
- 404 error when i try to search by category or by tag
- Make related posts display first in search result
- A search box for all sites – Multisite
- Unable to get all tags from specific categories
- category__and works, but why?
- foreach,having wp_query inside, breaks after showing one result