You can set the taxonomy query for the main query using pre_get_posts:
add_action( 'pre_get_posts', 'my_exclude_terms_from_query' );
function my_exclude_terms_from_query( $query ) {
if ( $query->is_main_query() /* && whatever else */ ) {
$tax_query = array (
array(
'taxonomy' => 'category',
'terms' => array( 'cat-slug' ),
'field' => 'slug',
'operator' => 'NOT IN',
)
);
$query->set( 'tax_query', $tax_query );
}
}
If tax_query
is already set and you need to modify it instead, you can grab and then add to the $tax_query
array.
Related Posts:
- When to use WP_query(), query_posts() and pre_get_posts
- WP_Query vs get_posts
- get_template_part in for loop
- pre_get_posts with get_posts
- “pre_get_posts” firing on every query
- get_posts() seemingly ignoring post_type
- Can not switch the queried post in pre_get_posts hook
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- How to change posts order on Category and Tag archives?
- Get posts for last working week in WP_Query
- Show Sticky Post at the top but do not show again in the loop?
- Can I set my WP_Query to be a Main Query?
- Show scheduled posts in main loop but not in WP_Query?
- Conflict with get_posts and the_content
- Best choice for multiple loop in page?
- WP Query – order posts by meta field first and then order the rest
- get_posts output always same post
- How I can change the condition or compare operator for WP_Query in pre_get_posts
- How to access $wp_query object from array of posts given by get_posts?
- Function using get_posts() with tax_query not working when called from functions.php
- Magazine style frontpage with multiple categories/loops and no duplicate posts
- Populate select list with meta values from all posts of a Custom Post Type
- Can I alter the main loop to ‘orderby’ a custom callback?
- Related posts queries
- Ordering Posts by parent category, name ascending
- Both WP_Query and get_posts returning 1 post
- tax_query not working properly with get_posts
- Get post count of current loop when using multiple queries on one page
- How to use the_posts_navigation for wp_query and get_posts?
- Display products from specific category in shop page
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- How to uniquely identify queries?
- Changing Posts Per Page and offset with pre_get_posts
- Is it possible to select against a post’s parent’s fields with WP_Query?
- Show two random posts from custom post type
- Add the “active” class only to the first loop item in a WordPress query [closed]
- Get posts by meta data OR title
- Using is_main_query to select custom post type on certain page
- How to pass custom parameter to WP_Query for filtering in pre_get_posts
- Loop inside the loop
- meta_query not working properly
- WordPress custom search form with pre_get_posts not work
- cloning a WP_Query
- How to query ‘posts_per_page’ to display a different blog posts index template?
- order by multiple meta keys in pre_get_posts
- How is WP_Query connected to WP_Post in The Loop?
- Trying to check and see if a post has a featured image outside of the main loop
- Using pre_get_posts on a specific core/query block
- Ignore post by meta value in the main query
- query user display_name failed in a custom mysql query foreach
- Query posts by specific word on title
- Get posts for which a custom field is not present, with get_posts
- How to choose between hooking into per_get_posts or into parse_query
- Multiple taxonomy And acf filter group by
- Reset postdata to custom query in nested queries
- How to change main query based on post meta
- How to Filter Posts by Custom Fields?
- Display posts from only one post form in custom query and exclude in main query
- Complex WP SQL Query
- add active class based on permalink and url
- Loop Split two Columns
- Filtering out child category posts from parent category archive not working
- Why does get_posts only show results for Admins or logged-out users?
- How to list posts with disabled/closed comments, with pagination?
- How to make a second query offset -2 from current post
- Hide elements outside loop based on query
- Wp_query with 2 meta keys and array of meta values
- meta_query: check if number exists
- How to create custom query by keyword in post title?
- Create a loop on my pages with new “WP_Query”
- WP ForLoop to compare meta information of posts to determine what post to display
- How to filter query loop block with a search string from the query parameters
- WP_Query – How to get all posts of specific days of week by custom field date?
- Different Loop for tag pages?
- 2 queries with counters
- Search.php – return number of results but cannot loop through
- Creating ‘posts page’ loop based on the page itself
- How to add date_query to meta_query array
- Make a products only viewable to a specific user ID in meta_query pre_get_posts
- Custom wp_query inside a conditional stament inside a template part doesn’t work: why?
- Duplice post with standard WP loop – fixed by using query_posts() instead
- Check the stored / cached WP_Query with transients on post change
- New template file does not load category-specific post
- Get categories within specific term
- Custom query for custom post type not getting correct post ID
- Query post with content only
- Why is my query not giving results?
- Continue loop after $queryObject
- Create a hierarchical loop at predefined markup requirements
- Display posts from catagories
- Adding a Theme Customizer Control for Posts_Per_Page
- WP_Query for attachments without duplicating post_parent and displaying tagged image
- List ALL posts within last week from current category on category page
- loop through custom post types with meta data
- Looking for a way to exclude frontpage and nav menu from query filter
- Using Query In Post Type Archives
- Multiple loops on index page with sticky post and pagination
- the_excerpt() not working in custom archive
- How to retrieve _embed (etc) fields in get_posts custom query?
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise