You’re nearly there, all that is left is to check on which page you currently are.
In the following code I’m first creating an array where the key corresponds to the page number and the value are the categories to exclude. This results in only one time writing $query->set(...
. And to be really sure that a value exists for the key, I added isset()
.
<?php
function excludeCat($query) {
$page = get_query_var('paged', 1);
$exclude = [
1 => '-3,-5,-23',
2 => '...',
];
if ($query->is_home && isset($exclude[$page])) {
$query->set('cat', $exclude[$page]);
}
return $query;
}
add_filter('pre_get_posts', 'excludeCat');
Related Posts:
- Homepage custom recent news
- exclude category from get_posts?
- Exclude the category from the WordPress loop
- Exclude certain category from latest updates
- Exclude categories from Loop, queries, widgets, post navigation
- List posts by category exclude current post
- wp_list_categories exclude not working
- Optimal way to redirect home page to category archive?
- How to Set an Individual Homepage for Each User?
- Excluding a category from next and previous post links
- Exclude Category From Home Page, Display Posts on It’s Own Page?
- How to exclude categories from recent posts, recent comments & category widgets?
- Exclude a category from the filed under list only on some templates
- display woocommerce all category title on home page
- How to exclude posts from a category when using this particular format
- Exclude category from Tag Template
- exclude post from displaying in loop if it is in a category, but not in many categories
- I need to create a search form that will display search results from specific category
- Exclude Posts From Specific Category from Next and Previous post links
- Query Posts Exclude Entire Category
- exclude categories from search results
- Exclude category from fucntion
- WordPress category widget only show categories with children
- wp_list_pages() exclude category
- Loop doesn’t exclude the specified category in home page
- Exclude category from query
- Functions.php: Exclude Category from “Blog”
- Posts from a category on homepage with category archieves page default css
- Woocommerce filter function not updating
- wp_list_categories not excluing multiple ids
- How to exclude posts from category posts count
- Exclude category from foreach loop
- Have posts that belong to multiple categories, exclude some categories from homepage
- Exclude all subcategories in the_category (post)
- I need to exclude from a query a category and a few custom taxonomies
- Shows only one Category in home page
- Exclude categories by ID
- Exclude the category in WordPress
- How can I exclude a category from the main loop with the category name & not ID?
- User level categories
- Exclude category from drop down list form
- exclude certain categories form archive widget
- pagination 404 error, same slug home and categories
- How to remove an item from a custom Walker_Nav_Menu
- Exclude a category and post_type from wp_query
- Exclude category from WP_Query args not working
- Displaying Category in sidebar post widget but not in the loop on home
- Category menu that filters out empty categories
- Display last postings of 5 categories on homepage
- How do I get a single page navigation depending on the previous page?
- Problems with explode [closed]
- Exclude Category From Home Page, Display Posts on It’s Own Page?
- How to have a category not show up in query post with page panigation?
- Duplicate homepage to show posts from 1 category
- Customize category query in widget
- Call function on a single page
- Country Ways Content Show
- Excluding cateory not working on my site. how to solve this? [closed]
- How to define category ID in an array?
- Remove parent category from permalink? Basically only have the child category?
- Filter by post category in Media Library
- Send an email when custom post type category is changed
- Why adding Categories does not auto refresh in Backend while using my custom theme?
- Remove specific category from a post
- How can I get an tag to wrap each ancestor that gets outputted in this condition?
- How to prevent empty category to list content of sub categories?
- Why would in_category only return one post in a specific category?
- Is it possible to have WordPress not recognize category URLs?
- Disallow categories from this MySQL query
- Use a wordpress page to display a certain category
- How to list child categories?
- Creating gallery with taxonomy images plugin?
- Show Posts from Single Category Not Working
- How to restrict category creation to admins only while allowing editors to still pick category
- Show child taxonomies (that have posts) of the current parent taxonomy
- Query (built in) Categories and Advanced Custom Fields?
- Get next post in same category not working WP 4.7.4
- query_posts by category_name and custom taxonomy
- Page that links to category content
- Query from multiple category (and)
- Wp Dropdown Category Not Show Current As Title
- How to load several hierarchical categories from functions.php
- How to get the deleted category id?
- Maintaining a separate posts page for certain categories and don’t show those on home page
- WP E-Commerce WPEC 3.8 Sort Categories
- How do I list only children of a specific category in a drop-down?
- Link posts together (relative, not grouped)
- How to hide description on categories and tags?
- Exclude a category from block editor’s category widget
- Replace deprecated get_category_children code with get_terms
- Retrieving Categories based on the first character of the name
- Woocommerce: all products of all categories instead products of one category
- Ajax load more not working with multipler categories
- Default category isn’t saved in database
- Help with Wp + Mixitup code
- Matching usermeta at registration to categories of content
- How do I hide posts in a category from all listings but still allow the posts to be viewed?
- How to show monthly archive posts?
- Function to reference post meta in place of category Woocommerce
- Category Redirection Problem