Try this:
function exclude_single_posts_cat($query) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( $query->is_archive() ) {
$query->set('post__not_in', array('1','2','3'));
}
}
add_action('pre_get_posts', 'exclude_single_posts_cat');
Issue: The issue in your first approach is you have added extra curly brackets and comma.
Related Posts:
- Sticky Posts exceed posts per page limit
- Theres a way to use $query->set(‘tax_query’ in pre_get_posts filter?
- Modify Taxonomy pages to exclude items in child taxonomies
- Obliterate the main query and replace it
- Post injection – how to exclude the original post
- Adding meta_key via pre_get_posts causes navigation to disappear
- Sorting posts that has a meta value first then the rest of the posts
- pre_get_posts on a page
- How pre_get_posts filter by roles in WP Admin
- Weird problem with pre_get_posts and $query->is_page()
- Why is ‘pre_get_posts’ having no effect?
- Date Query to Pull Current and Future Posts
- pre_get_posts all posts and custom post type with certain tag
- Using Sessions to Filter Posts – bad thing?
- pre_get_posts and the blog page
- Archive by custom post type and custom date field
- pre_get_post filter returns results when there should not be
- Trying to exclude first 5 posts from the first page on the homepage
- Exclude a WordPress post from pre_get_posts if a field is null
- Why the ‘date_query’ is not working in ‘pre_get_posts’ hook?
- Only show certain post types in recent posts widget
- pre_get_posts for exclude category
- pre_get_posts: using tax_query only for certain post type
- pre_get_posts dont firing… Anybody knows whats the wrong with my code?
- Restrict Search Query To After Specific Date
- WP_Error not displaying errors
- Show all posts even if URL points to a single one
- How to achieve post_status__not_in?
- Ordering by meta_key
- Using a pre_get_posts filter to search for multiple strings on all meta values
- Removing taxonomy query by pre_get_posts
- Using different parameters for different queries with pre get posts in functions.php
- how to restrict posts_request filter to the main query only
- Automatically applying a pre_get_posts filter for child categories only
- How to override a query and display specific page by ID?
- pre_get_posts variables
- Another query in pre_get_post cause memory issue
- pre_get_posts having no effect on my category archive
- pre_get_posts returns non property object when using posts__not_in
- Override tax_query with pre_get_posts to include other term_ids on a single category
- $query->is_main_query() is causing query’s tax_query to be ignored
- Menu disappears when meta_key changed [closed]
- pre_get_posts with multiple queries
- How to show the last and newest modified post in a custom category?
- Altering the main query using get_post_meta() in pre_get_posts
- Why query by specific date with variables doesn’t return same result that with harcoded integers?
- Sort WordPress Archive by multiple oderby arguments in pre_get_posts action
- Exclude post type with pre_get_posts?
- Exclude page by title for non admins
- problem with setting tax_query in pre_get_posts
- Modify Taxonomy pages to exclude items in child taxonomies
- Modify author archive query to combine two queries
- Change post order on archive to be displayed by most commented being ignored by theme
- Complex query using pre_get_posts
- Super confusing ‘pre_get_posts’ behavior with $query->set
- Having trouble with settings terms as array in pre_get_posts
- Override main query for page template
- Querying custom taxonomy on category-specific page is overwritten by function
- how to get content from other site and show it?
- Insert a variable in pre_get_posts
- Extend taxonomy term page with other posts
- Set a custom number of posts on the first page
- Why does this query not SELECT post IDs like a normal query would?
- Please ensure me that I’m not crazy using the pre_get_posts [closed]
- How to pass >= condition filter to my year custom tax_query
- What is the proper way to use pre_get_post?
- pre_get_posts has php notice when not on CPT archive
- Duplicating event posts in wordpress
- Using pre_get_posts to rewrite search query to display posts from multiple taxonomies
- WordPress Number of Posts Not Changing With posts_per_page
- Show only posts which can be commented
- How do I edit pre_get_post for the category the user is currently in?
- How to sort posts in admin by titles with dd.mm.yyyy format?
- Building an Advanced Search (text, tags, category, custom fields) – Getting the wrong SQL query
- Category archive page – loop through posts of certain tag (with pagination) – pre_get_posts
- Two loops by pre_get_post on same page
- Including only current user’s posts in search
- How to sort custom post type posts in default order by multiple fields?
- Show Sticky Post at the top but do not show again in the loop?
- hide custom post types with specific meta key ON admin backend
- Woocommerce: Complex query in pre_get_posts [closed]
- pre_get_posts – Trying to get property of non-object warning
- pre_get_posts : only get posts by wp_usermeta value
- Compare meta key to current date in pre get post
- Multiple meta_key ordering with pre_get_posts
- pre_get_posts works in post type archive but not in single post
- How to apply pre_get_posts to a custom query?
- Excluding posts from search results page with meta query not working
- pre_user_query vs pre_get_posts
- Can I alter the main loop to ‘orderby’ a custom callback?
- How to exclude woocommerece product category in search results?
- How to show only specific category post by user role without plugin and restrict all other cats
- Apply pre_get_posts filter in a certain moment (just for a particular block of posts)
- Change post category within pre_get_posts
- CPT year wise archive based on custom date Field
- WP search in metadata post
- Fetch post meta in the same query as the main loop
- Filter WP_Query output before it is accessed (pre_get_posts)?
- Pre_Get_Posts order DESC not registering
- Custom taxonomy with custom meta value is not sorting correctly (query returns the same value for orderby regardless of sort column click)