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
- When to use WP_query(), query_posts() and pre_get_posts
- When should you use WP_Query vs query_posts() vs get_posts()?
- Should I use Pre Get Posts or WP_Query
- Can I force WP_Query to return no results?
- Using pre_get_posts with WP_Query
- Resetting post data to previous loop in nested loops
- Some doubts about how the main query and the custom query works in this custom theme?
- Get post count of current loop when using multiple queries on one page
- Multiple WP_Query loops with Pagination
- Using a custom WP_Query with get_template_part loop
- WP_Query orderby post__in remains ineffective in the Loop [closed]
- How to use the_posts_navigation for wp_query and get_posts?
- What is “main query”? [duplicate]
- Loop within a loop?
- WP_Query and next_posts_link
- Show posts without term
- meta_query: using BETWEEN with floats and/or casting to DECIMAL
- Usage of the new “posts_clauses” filter in WordPress 3.1?
- Get posts from Network (Multisite)
- Display products from specific category in shop page
- How to list some posts first in the loop based on post id
- How to order by post_status?
- Using get_posts vs. WP_Query
- Delete all posts from WordPress except latest X posts
- Reduce or prevent calling of update_meta_cache
- How to add taxonomy filter on the query fly?
- Pagination returns 404 after page 20
- Make loop display posts by alphabetical order
- WP_Query: query posts by ids from array?
- Use WP_Query with have_posts()?
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- Sanitation needed for WP_Query or get_posts calls?
- Differences Between WP_Query() and get_posts() for Querying Posts?
- get custom post type by tag
- How to get order of posts?
- Order posts by ID in the given order
- How to uniquely identify queries?
- Get Recent Posts by Date in Multisite
- Order Search Results Page by meta_value If no Value Return Remaining Results
- get_posts with multiple categories
- Too slow when using both ‘tax_query’ and ‘meta_query’ both in WP_Query
- Use of caller_ get_ posts
- Get array of posts from the current archive page loop
- Pagination with WP_Query is buggy – working for some pages, but not the others
- Get the number of posts from the current page results
- Changing Posts Per Page and offset with pre_get_posts
- How to exclude latest x posts from a paginated query?
- Executing Queries in tag.php
- Display posts the match taxonomy term linked from wp_list_categoies?
- How can I save an array from a random post sequence for later use?
- Is it possible to select against a post’s parent’s fields with WP_Query?
- How to place a loop within another loop?
- WordPress Custom Query to show posts from last x years
- Query all posts where meta value is empty
- How to order posts tag by tag?
- Slow SQL_CALC_FOUND_ROWS Query
- Query Custom Meta Value with Increment
- Show two random posts from custom post type
- RSS feed with specific keyword
- Add the “active” class only to the first loop item in a WordPress query [closed]
- Loop through all tags & output posts in alphabetical list
- Get posts by meta data OR title
- Pagination with 5 posts per page
- Identify which loop you are hooking into; primary or secondary?
- Get_post() with meta_key when compare is a date
- Using is_main_query to select custom post type on certain page
- get_posts not finding argument: post_name
- Meta Query with date and time on the same Day before given time
- In loop: posts have thumbnail AND other variables
- Multiple orderby parameters in pre_get_posts() action
- Group posts by custom field
- Get posts with condition on comment meta value
- Retrieving 3 latest post from each of 5 different custom post types