Yes your code will fail, but not because the above answer, but because this isn’t the page you ask for.
As you wrote, you set the page “videos” to be the page where all the posts are shown.
When a page is set as a blogpage, WordPress will use home.php or index.php according to the template hierarchy to display the blogpage, not a page template. This page is regarded as the home page, so is_home()
will return true on this page
So, to exclude the categories, you need to use this conditional:
function exclude_category($query) {
if(is_home()){
$query->set('cat', '-42, -43');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
Happy Coding,
Kuchenundkakao
Related Posts:
- Exclude Tags from get_the_tags
- is there a quick way to hide category from everywhere?
- Exclude category from category archive
- Multiple arrays in post__not_in parameter
- Don’t execute function on specific posts
- using pre_get_posts to exclude
- problems exluding categories
- Better way to exclude category output for post/pages?
- Disable the link on certain pages
- post__in and tag__not_in conflict
- How to exclude one category
- How to exclude a category in the following Code?
- Exclude Categories from wp_list_categories()
- List post wit the same category but exclude parent
- Display 1 category only with get_the_category (by ID or slug)
- trouble with my loop
- Get first category only and excluding one
- Exclude post ID from wp_query
- Dynamically exclude menu items from wp_nav_menu
- How do I exclude plugins from getting automatically updated?
- Exclude Current Post from Recent Posts Loop
- exclude category from get_posts?
- Exclude Specific Term from Search
- Exclude the category from the WordPress loop
- Skipping first 3 posts in wp query
- Exclude certain category from latest updates
- how can I use WP_Query to exclude a specific tag.?
- Exclude categories from Loop, queries, widgets, post navigation
- List posts by category exclude current post
- wp_list_categories exclude not working
- How to filter by category in REST API, excluding posts also in other category term?
- Exclude ALL posts from sub categories
- Excludes posts that don’t have attachments in the_content()
- Excluding a category from next and previous post links
- Retrieving custom taxonomy in order, but excluding specific tax IDs
- How to exclude/filter a tag from get_the_tag_list()
- Exclude post_type from admin comments_list
- Exclude Category From Home Page, Display Posts on It’s Own Page?
- Exclude Custom Post Type & Pages From Auto-Tag Function
- Add exception for specific tag
- How to exclude categories from recent posts, recent comments & category widgets?
- Exclude a category from the filed under list only on some templates
- Get attached media only
- Alter secondary loop to exclude posts from current page category
- Exclude posts with specific meta_value while sorting by a separate meta_value
- Exclude file from theme editor
- Filter keywords from search query
- Exclude pages with certain template from wp_list_pages
- exclude custom post type by meta key in wp_query
- Dynamically excluding current page id
- Do not allow to search certain words
- Exclude a specific tag from the get_the_tags list
- Exclude One Category and its Subcategories using WP_LIST_FILTER
- Exclude the parent custom post-type posts only
- Exclude subfolder from WP-redirect works with html but not php files
- How to exclude shortcode from specific page IDs if it’s set to global
- Query Ignoring ‘exclude’ Parameter?
- How to exclude a directory from WordPress permalinks in a Multisite environment?
- Filter Posts by Excluding Categories
- Default WP search excluding specific characters, is it possible?
- How to set post expiration date and time and move the page to archive after expiration [closed]
- How do I exclude posts by custom field value?
- Exclude specific taxonomy term when using wp_get_post_terms
- Exclude custom taxonomy tag from loop
- How to exclude posts from a category when using this particular format
- is there a way to retrieve posts that do not have a featured image assigned?
- WP_Query Excluding pages with Order is 0
- Exclude category from Tag Template
- Query: offset post list, unless it’s a specific category
- exclude post from displaying in loop if it is in a category, but not in many categories
- How to query posts to include specific pages of one post type, and all pages of another post type?
- pre_get_posts : ‘post__not_in’ doesn’t work with global variable
- I need to create a search form that will display search results from specific category
- how to get posts ids inside pre_get_posts filter?
- Exclude Posts From Specific Category from Next and Previous post links
- Exclude featured image from attachment loop
- How do I exclude the current post from listing in recent posts
- Query Posts Exclude Entire Category
- exclude categories from search results
- Exclude category from fucntion
- Exclude featured image from gallery code stops Lightbox from working
- Exclude featured image and custom field from this get_attachment query
- How do I exclude recent post from recent post php widget
- WordPress category widget only show categories with children
- Remove ID page from wp_count
- Combine get_page_by_title to exclude pages from wp_list_pages
- Individual post. Meta_key, taxonomy or post status for separation?
- Excluding pages in WP_query using ACF
- Next Posts Prev Posts for Standard post format only
- Exclude Category filter from Portfolio section
- ‘exclude’ argument is not working with get_posts for a custom post type
- Avoid repeated post on page 2 when excluding one on page 1
- Exclude custom taxonomy term posts from custom post loop
- Exclude the first ‘n’ number of posts of a tag from home page?
- Exclude image titles from search results
- Exclude posts with taxonomy term from appearing on home page query
- Exlude pages from wp_list_pages
- Exclude all sticky posts front page twenty twelve
- Exclude post format from get_posts
- Excluding posts not working