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?
- exclude certain categories from the list at the end of the blog post
- Exclude category when displaying all categories assigned to a post
- Sorting past events by month
- Taxonomy page wpml dropdown not working
- how do I exclude a category foreach
- sub-category showing up in wrong order in breadcrumbs
- How to get all Existing Categories from a Custom Post Type
- category_count gives wrong number of posts in category
- How to get specific number of category post on a page or category page?
- Sort Category Archive pages and place sticky posts first
- Use same wordpress category base and tags as the prefix as the post permalink
- How to add custom prefix before category base for category page URL?
- Is there a function to modify existing previous/next post link without modifying theme files?
- Add custom fields to Woocommerce Category Page
- How to hide my categories pages’ names, and how as well to reduce the space beween my sidebar’s widgets?
- Category Redirection Problem
- Hierarchical outline of posts
- wordpress category rewrite rule with pagination
- WordPress how to remove parent slug from category URLs
- Display different sidebar based on search results from one category
- Adding category to existing post (uploaded file)
- Add Input Form for Select Category Like Tags
- CPT Custom post type Tags & Categories not showing for ‘Editor’ user in editor
- how to create a page that shows all of multiple category posts on a single page
- Quick Edit on taxonomy names results in error
- Can’t create tags or categories
- Invalid login email notification for password-protected category
- Exclude a category ID from the following function
- Add custom text in second paragraph of posts in specific category
- How to remove the category from the url only for a specific category of articles on wordpress
- Restricted category in Woocommerce [closed]
- How to make /category/ URL load properly instead of 404 error?
- Excluding a category from frontpage but not from WP_Query
- Fetch Post Category
- Remove product meta from single product page
- How to get parent and child sub category (Taxonomy) order to display
- Categories Not Showing in breadcrumbs in posts
- WordPress Related Posts by Title and Category
- How can I get the categories and subcategories separately?
- Keeping posts in Categories
- My Website Having Duplicate Category Titles On 1,2,3 pages