You’ll find an appropriate filter in the get_terms
function which is called by get_categories
.
See source code https://github.com/WordPress/WordPress/blob/master/wp-includes/taxonomy.php#L1247
We don’t use any of the callback arguments in this case. We can use array_filter
on the list of terms (WP_Term) objects. See https://developer.wordpress.org/reference/classes/wp_term/
This allows us to remove a term with a particular ID
add_filter( 'get_terms', function( $terms, $taxonomies, $args, $term_query ){
return array_filter($terms, function( $term ) {
return $term->term_id !== 2;
});
}, 10, 4 );
Note that get_categories
by default only returns categories which have posts assigned, you can change that by adding hide_empty => false
to the $args
array.
Related Posts:
- Filter categories using tags
- Make parent categories not selectable
- Add custom category name as data-filter to switch between these categories
- Can paginate_links() be customized for a specific category or tag?
- Filtering by Category doesnt paginate correctly on the backend
- Dropdown category filter
- Make parent category not selectable when it has child categories
- Replace category titles
- Hide posts of a certain category unless logged in
- What’s the Simplest Way to Override/Rewrite the %category% Permalink Structure Tag?
- How to Filter categories in the permalink structure
- How to display posts under a specific subcategory of a given category
- Taxonomy filter all children
- Filter Home Page Posts in Buddypress [closed]
- Categorising posts/pages into years published
- Inserting Category programmatically
- Automatically set default password to all posts in a specific category
- get_the_categories filter returns an empty array
- category filter doesn’t work in WPML
- Query posts from category based on a filter most favorited
- Filter Categories in meta box for CPTs
- Show only first category name in RSS?
- How to organize custom categories for filtering and selecting
- Filter $cpt categories to omit certain category
- How to set a filter by category within a customised page
- Refine/Filter the Search Results by Category
- Admin comments and user restrictions
- WordPress Post # of # filtered by category slug
- Pagination for ajax category filter
- ajax for filtering posts by category in wordpress loops
- How to hide category name
- How to make the ‘delete’ button inactive on some categories?
- Overriding default calendar to show posts from a category
- How can I display tags as categories?
- Display all product tags associated with a specific product category
- Filter WooCommerce archive pages by an additional category
- Portfolio Filter Buttons Wont Work
- “Virtual category page” based on a custom field filter
- Custom tax_query filter not working for Woocommerce product categories
- How to hide some categories in dashboard
- Change post title if post has specific category
- Filter By Category Dropdown List With Custom Taxonomies
- How can I filter posts by tag on the fly
- Change Default Content when Creating a Post based on Previous Category Choice
- Maintaining a separate posts page for certain categories and don’t show those on home page
- Exclude posts without category from loop
- How to filter post from categories only if the posts appears in one of them, not the other?
- How to fix select filter on category
- wp_dropdown_categories() works correctly but the list is not filtered in admin for custom post type. What is the problem?
- Add $args to wp_list_categories
- How to hide a category or tag everywhere EXCEPT for use with get_posts
- Add filter for specific category only
- How to create category filter on a blog like on the site below?
- How to filter tags by category?
- Custom Taxonomy Isotope Grid – Filter Buttons Not Working
- Can’t display posts by filtering categories using isotope.js
- Correct Hook/Filter to amend category choices on post edit page
- Restrict retrieved terms by category?
- Hiding by default posts in given category except for some cases
- Remove “Category:”
- ACF Gallery with Media Categories – how do display categories and sort
- Custom filters inside a specific category?
- filter single_cat_title avoiding the breadcrumbs
- Right way for setting categories
- Custom built theme won’t filter categories
- category filter doesn’t work
- How to create a photo gallery that can be filtered by the image categories
- Remove “?category=” in the URL wordpress ajax filter work for category
- Replace category titles inside RSS feed
- How can I hide a category from Contributors in the edit/add new post screen?
- Multiple post categories – single permalink
- Display all posts with same title
- Show children of top level category only
- Add custom taxonomy under the category title
- Looking for the best solution to build this Blog structure? Picture attached?
- How Can I Display Categories Description
- Specified number of categoy posts not showing correctly
- Filtering multiple categories via the URL
- How to limit number of attachments
- Show category name and new articles and display page numbers. What function to use
- Sub category templates
- How to add custom color fields to the category edit page?
- Target a specific category from a custom taxonomy?
- How to modify the output of wp_terms_checklist when used within the built-in category metabox on edit posts?
- How to get nav to show current_page_parent class when on regular post (not blog), differentiated by category
- How do I export the headlines of all the blog posts in a single category?
- Category post count is not correct
- Regarding Tags And Categories
- selecting categories that have products with tag/category
- How to get WooCommerce Product Categories and its children’s children?
- Media Library Category Exclude Tree
- How to display child-category PAGES on a category page
- How get all media from a posts category by db?
- Cant get unique_array() work on get_the_category() foreach loop
- Show posts from all categories
- how to Specifies an author / editor to edit one category only?
- My Query is getting the wrong data
- Display post content from category name using ajax
- How to display the category featured images [closed]
- Echo Category In Loop