try to implode $category with “,” as a glue. Mentioned code in each foreach run rewtites the cat variable of $query.
$query->set('cat', implode(',', $category));
Related Posts:
- Exclude Tags from get_the_tags
- is there a quick way to hide category from everywhere?
- Exclude category from category archive
- pre_get_posts : ‘post__not_in’ doesn’t work with global variable
- how to get posts ids inside pre_get_posts filter?
- Multiple arrays in post__not_in parameter
- Exclude some categories from the post page
- Don’t execute function on specific posts
- 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
- How to exclude first 2 posts from a specific category for a custom post type archive page
- When to use WP_query(), query_posts() and pre_get_posts
- Sort on meta value but include posts that don’t have one
- Exclude post ID from wp_query
- Can I exclude a post by meta key using pre_get_posts function?
- Should I use Pre Get Posts or WP_Query
- Using pre_get_posts with WP_Query
- Sticky Posts exceed posts per page limit
- Dynamically exclude menu items from wp_nav_menu
- Using pre_get_posts on true pages and static front pages
- Theres a way to use $query->set(‘tax_query’ in pre_get_posts filter?
- Modify Taxonomy pages to exclude items in child taxonomies
- How do I exclude plugins from getting automatically updated?
- Exclude Current Post from Recent Posts Loop
- WP_Query orderby post__in remains ineffective in the Loop [closed]
- How to Change Order of Posts in Admin?
- Media library to list images only user uploaded
- multiple orderby in pre_get_posts action
- What is “main query”? [duplicate]
- Prevent pre_get_posts filter on specific post type
- Obliterate the main query and replace it
- Using pre_get_posts to rewrite search query to display posts from multiple taxonomies
- Post injection – how to exclude the original post
- sort child pages on admin
- Custom Table Column Sortable by Taxonomy Query
- Include and Exclude Taxonomies From Archives & Feeds Using ‘pre_get_posts’
- Custom search filter causes menu and query_posts problems
- exclude category from get_posts?
- Exclude Specific Term from Search
- Using meta_query on pre_get_posts to exclude a particular meta_key value
- Reduce or prevent calling of update_meta_cache
- How to add taxonomy filter on the query fly?
- Exclude the category from the WordPress loop
- Adding meta_key via pre_get_posts causes navigation to disappear
- is_category() in pre_get_posts strange error
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- $query->set in pre_get_posts is unintentionally affecting the backend
- Skipping first 3 posts in wp query
- pre_get_posts with tax_query causes empty result
- How to uniquely identify queries?
- Show only posts which can be commented OR have custom meta field
- Order Search Results Page by meta_value If no Value Return Remaining Results
- complex restriction of items in media library
- Using pre_get_posts to set posts per page, how do I?
- How to put custom post types on front page
- Archive Listings Filtered by Date Values in a Custom Field/Post Meta?
- Exclude certain category from latest updates
- pre_get_posts with get_posts
- How-to exclude terms from the main query the most performant way?
- WP_Post_List_Table::get_views – Have post counts account for filters?
- how can I use WP_Query to exclude a specific tag.?
- Changing Posts Per Page and offset with pre_get_posts
- Add Custom Post Type to Current Query
- How to exclude latest x posts from a paginated query?
- Does pre_get_posts affect REST API responses?
- Exclude categories from Loop, queries, widgets, post navigation
- How to show more posts on an archive page?
- Sorting posts that has a meta value first then the rest of the posts
- How would I use pre_get_posts to query another site for posts in my multisite network?
- Include post id[s] into WP_Query()
- Search ONLY by meta key / meta values
- Pre get posts for single post
- Custom post type by ID in root folder
- Sort archive view with pre_get_posts hook
- Slow SQL_CALC_FOUND_ROWS Query
- “pre_get_posts” firing on every query
- List posts by category exclude current post
- RSS feed with specific keyword
- wp_list_categories exclude not working
- How to filter by category in REST API, excluding posts also in other category term?
- How to get my Custom Post Type to display in Recent Posts using “pre_get_posts”
- pre_get_posts on a page
- pre_get_posts OR relation between taxonomy and author
- Using is_main_query to select custom post type on certain page
- Exclude ALL posts from sub categories
- Meta query interfering with orderby relevance
- Add custom field (value) to search result (without plugin)
- How pre_get_posts filter by roles in WP Admin
- Custom query filter not working on woocommerce category page
- Varying the number of posts per page from the first one
- Multiple orderby parameters in pre_get_posts() action