There is even an example in the WordPress codex here for this:
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-3,-8' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
Just modify the category IDs (replace -3,-8 by your category IDs preceded by the minus sign) and put it in your functions.php or your plugin code.
The WordPress codex states the following:
query_posts()
is the easiest, but not preferred or most efficient, way to alter the default query. The preferred way is hooking intopre_get_posts
and altering the main query.
This way you just alter the main query instead of throwing away the results of the first query and running a second.
Related Posts:
- 404 Error On Category and Tags Pages
- Block multiple categories from Blog
- If newest post of category is newest post in general, skip first post of category
- How to create button to direct to certain category
- WordPress Genesis not excluding category from blog
- Putting a Category of posts under a Page
- Functions.php: Exclude Category from “Blog”
- How can I display tags as categories?
- Display content from a specific category using cat ID not working
- Display subcategories in dropdown
- Hide a category of posts from main blog, and only show in category view
- Add code to header based on category for specific posts?
- Use blog as base for tags and categories
- Restrict CSS on one page
- Rebuilding a Site: How to Show Several Category Posts in Woo Framework Theme?
- excude a specidic category from showing in the “relative posts”
- Category post count is not correct
- How to create category filter on a blog like on the site below?
- Blog page with posts from specific categories
- Is it possible to set up blog categories without changing permalink structure
- Redirect Category pages to blog page with query string
- Read More showing only on first post
- List post categories that link to a page showing those posts
- Pages, Categories, and working with someone else’s code
- Multiple Categories under one URL, where ‘Front Page’ is used already
- Restricting certain blog posts by date?
- How to get permalinks with category base working with sub-categories
- WP 3.9 TinyMCE no longer loads on category description editor
- Change Gutenberg category checkboxes to radios
- Exclude categories from search query
- Why is per_page not working with categories in WP API?
- How to order the get_categories result
- List posts by category exclude current post
- How can I calculate the total number of categories at different hierarchy levels?
- In the tag archive – display count of posts for each category they belong to
- Display only deepest category on a single post?
- How we add new categories by wp_insert_post
- Listing all posts from current category on page
- Hide uncategorized products from the shop page
- I want exclude the particular category in sidebar
- wp_list_categories() – adding a div to each li?
- How Can You Exclude Categories From Your RSS Feeds?
- Help with multiple dropdown tags search
- How to get category URL with the slug?
- weekly archive for custom category
- ?cat=-1 Indexed Versions Of Homepage
- .htaccess too many redirects based on category slug
- How get permalink for the current category or tag?
- using checked function to verify value against an array
- wp_list_categories depth and number
- Category index featured image
- Order categories by most posts
- need to add custom field in more products
- How to redefine the sorting of product categories by menu_order?
- Custom archive.php: retrieve right post categories?
- Add category only if post has custom taxonomy category
- Restrict author to create subcategory in a selected category
- Get posts from 2 different categories
- echo category description in single.php
- How to automatically check multiple categories on new post
- How do I get the parent category if I know the child category?
- Hide specific category from wp_list_categories
- ow to display the number of posts in a category
- Force category choice before creating new post
- How to modify the “View all posts in category” title attribute
- Show post categories
- Exclude categories by ID
- Add description to categories menu in admin
- Display Sub-categories or Child Pages of Parent Cat/Page?
- How to check if a categoryID exists in a theme options page
- How to display product categories and number of sales on WooCommerce
- Display one category and its suncategories
- How to differentiate the homepage structure from the category page structure in WordPress template?
- Placing category specific banner/html at top of respective category pages
- How to remove category name for only one category?
- Get parent category field from child category [closed]
- Custom sidebar isn’t showing up on sub-category posts
- Can a Woocommerce product category URL contain “&”?
- How can I display all products from all categories on one page with woocommerce?
- Unable to create pagination for Category.php
- Function add class to second menu specific items if the page has a certain category
- Posts says they are uncategorized but they are not
- How to call Primary Category for WordPress Woocommerce
- Meta Box: display single image on an archive-template
- Search Replace Database ONLY for posts of certain category?
- Categories Help and video uploads
- Display only children of custom hierachial taxonomy
- Filtered post assigned to categories
- Paginations in Category Page Leads to Sample Pages
- Allow a user to ‘subscribe’ to different post categories?
- If on Category Page add “active” class?
- Transfering static site to wordpress and retaining category by month paginations
- Show only one category in main query, issues on tag page
- Bringing Ajax Loading Feature on Category Pages
- Show category-ID in custom category-list
- Dynamically Adding Category Specific Content To Specific Category & Subcategory Pages
- Structure with category setting
- Do not show all post from all categories, just show posts from category in current loop
- How to prevent page re-sizing for mobile browsers [closed]
- Restricted category in Woocommerce [closed]