The quick take would be something like this:
$categories = [ ];
$parent_categories = get_categories( [ 'parent' => 0 ] );
foreach ( $parent_categories as $parent_category ) {
$id = $parent_category->term_id;
$categories[ $id ] = wp_list_pluck( get_categories( [ 'parent' => $id ] ), 'term_id' );
}
The important bit is a parent
argument, which limits retrieved to immediate children (parent of 0 is a top level).
Depending on how many of these you have it might be preferable to instead retrieve all of them first and then re-arrange out of singe result set.
Related Posts:
- wp_list_categories: order by term order?
- Difference between get_category, get_term_by and get_categories
- get_terms from registered taxonomies not working
- Display custom taxonomy on single post
- Getting all categories even with no products under it?
- Add HTML to Term Description
- How to create a array that contains all of the children slugs of a specific parent category
- List posts by their custom taxonomy term, with a condition
- I can get posts in all categories. But I want to get the custom post type I created
- how to show all categories by get_terms
- Is there any WordPress way to get all categories for some selected posts?
- Is There a Difference Between Taxonomies and Categories?
- What is ‘term_group’ for ‘order_by’ in get_terms()?
- Check if a post is in any child category of a parent category
- How to export and import taxonomies (category, tag and/or custom taxonomy) and their terms
- Order get_terms by term meta
- Get parent id by term id
- When to / not to use wp_get_post_terms vs get_the_terms?
- Why does WordPress combine a term with the same name in the wp_terms table?
- Child terms from multiple parents?
- Programmatically create product category and add thumbnail in woocommerce
- How to order the get_categories result
- How do I access a single term from a post?
- get_post_terms not working as expected
- Get terms that are associated with products from current category
- List all categories but exclude current post category on single post page
- has_term() does not return when term is assigned post?
- get_terms() but with additional dimensions?
- Count argument in get_terms has no effect on ouput/doesn’t work
- Action hooks returning old category instead of new category
- Wp set post terms not work
- get_terms only show term if there is a post using it
- get_terms won’t display product_cat or any other custom taxonomies when specified
- get / list categories and counts filtered by custom taxonomy term
- Reduce / optimize calling of wp_get_object_terms() when generating permalinks
- How to trace/fix false $term->count, rogue term relationships?
- List Terms in Category
- Do I have to set parent when set post term?
- How to get data about category in category.php
- 404 on term taxonomy archive pagination only with some terms
- Clone Terms of one taxonomy to another
- Get random out from get_terms()
- Conditional based on number of specific custom taxonomy terms in archive.php
- Display Posts Loop for each parent and for each Child
- Faceted search with WP-API data
- Show List of Terms (not posts) that have been Recently Updated?
- Get posts from specific taxonomy term
- Query to get term id using post id?
- Getting category URL with hyphens, not spaces
- What does ‘category__in’ mean in queries?
- Get all terms linked to a post and include those who not connected from get_terms and sort
- single_term_title() running before get_the_title() [closed]
- Get terms within a custom taxonomy
- How to get terms of not only the current cat but also of all its children
- How to get the $meta_type given the $object_id
- How to Link to Most Recent Custom Post of Same Term
- Getting terms to have space between them
- Custom term templates
- INSERTING Data into table with placeholders
- Exclude categories by ID
- WordPress custom taxonomy
- Retrieve Custom Taxonomies with Description and Slug
- Post to inherit custom category background image from parent
- How to check if a term is parent to another?
- Combining these queries? (Same taxonomy term + same category as post)
- Create Terms on taxonomy by text field
- Customizing the wordpress tag-cloud output
- get_object_term_cache duplicate terms after update to wordpress 6
- get_terms() delivers wrong count with custom taxonomy childs on custom post type
- Sort categories by custom field in WordPress admin
- Replace deprecated get_category_children code with get_terms
- Cannot get to work tax_query array for terms
- Restrict retrieved terms by category?
- wp_set_object_terms() to update a post taxonomy value
- Order get_terms by multiple values
- Get All Taxonomy Terms From Get_terms Array With Specific Array Key
- Confused by get_the_terms to use in a new wp_query
- get_terms for all custom taxonomies shows invalid taxonomy error
- Change the last separator in the_terms
- How to show only tagged CPT categories / taxonomies for a custom post type?
- show only terms from parent category
- Order terms by creation date
- Unable to retrieve any child terms using get_terms
- WordPress using get_term to retreive slug not working as expected
- Importing custom DB table entries as posts with Categories
- List Terms by category
- list of tags overlay
- get_adjacent_post_link excluded_terms ignored
- Getting Term_id – Taxonomy Metadata plugin
- store an array of all the terms existing
- Get only last child category link of products
- Get Parent Custom Taxonomy Term and Color Div background
- exclude parents from the_terms
- get_the_term_list() display in ul li and remove tag
- Custom field drop down to set the term of the post when published
- Incomplete term slugs output from a foreach loop with get_the_terms
- How we retrieve the ‘tags’ and ‘attachment’ of the custom post which is created by wp_insert_post();
- hide_empty property not working when using get_terms
- How to get tags only with custom meta field and display them randomly?
- Woo – Change order status by category [closed]