I found a solution to this myself. It returns the default post type posts instead of excluding the custom post type which is fine for my needs.
function my_breakfast_query ( $query ) {
// not an admin page and is the main query
if (!is_admin() && $query->is_main_query()){
if (is_tax( 'food', 'breakfast' )){
$tax_query = array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( 366 )
),
array(
'taxonomy' => 'food',
'field' => 'id',
'terms' => array( 364 )
)
);
$query->set('post_type','post'); // Added this line to get a working solution
$query->set('tax_query', $tax_query);
}
return $query; // Added this line to get a working solution
}
}
add_action( 'pre_get_posts', 'my_breakfast_query' );
Related Posts:
- pre_get_posts: using tax_query only for certain post type
- 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
- problem with setting tax_query in pre_get_posts
- How to pass >= condition filter to my year custom tax_query
- 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
- pre_get_posts with tax_query causes empty result
- 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()
- Order posts by (hierarchical custom) taxonomy terms and term children
- Why is ‘pre_get_posts’ having no effect?
- tax_query not working on main query
- 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?
- WP_Tax_Query with post_tag not working
- Only show certain post types in recent posts widget
- How do I sort posts by custom taxonomy?
- pre_get_posts for exclude category
- pre_get_posts dont firing… Anybody knows whats the wrong with my code?
- pre_get_posts Remove tax_query Completely
- 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
- Tax query in pre_get_posts not working
- Custom Taxonomy Not Working When added to WordPress Search
- 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
- Tax query with multiple terms in pre_get_posts
- Menu disappears when meta_key changed [closed]
- pre_get_posts with multiple queries
- Custom tax_query filter not working for Woocommerce product categories
- 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 page by title for non admins
- Tax query get first product with attribute value in pre_get_posts
- Problem ID to exclude specific posts from category
- 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
- pre_get_posts main_query not displaying after applying tax_query filters
- 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]
- 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
- Does tax_query really beats meta_query in all situations?
- Hidden woocommerce products still showing up in search results [closed]
- Combine tax_query and meta_query in WP_Query
- How to alter the query using pre_get_posts hook and is_post_type_archive
- Query Multiple Taxominies Across Multiple Post type’s
- Get taxonomy parent using child slug (from merged taxonomies)
- Fetch posts that match term slug first two letters (wp query)
- how to show records that don’t have custom meta value
- Elementor posts custom query based on tag [closed]
- Hi need update in my terms for other taxonomy
- Sort by DESC works, ASC doesn’t?
- pre_get_posts pagination not working
- Can I create a tax query to return posts that match two slugs from same taxonomy
- how do you pull data from two taxonomies?
- Tax_Query not working – connecting to second wordpress database
- Set different posts_per_page for custom post type/taxonomy
- why doesn’t this pre_get_posts code work?
- pre_get_posts with WP_Query to prevent posts from specific tags
- Setting a custom $query->query_vars[‘meta_key’] breaks the WordPress menu
- Set posts per page for parent category and it’s all children