Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rustam-swe/d20837c6e077b11516c3141a028283b7 to your computer and use it in GitHub Desktop.
Save rustam-swe/d20837c6e077b11516c3141a028283b7 to your computer and use it in GitHub Desktop.
Wordpress snippet to get all categories except the first level
$args = [
'taxonomy' => 'product', // Taxonomy type
'hide_empty' => false, // Display all categories including empty ones too
'exclude' => 15 // ID of default, uncategorized category or any taxonomy (optional)
];
$topLevelCategories = get_categories($args); // Query to get all categories
$childCategories = getChildCategories($topLevelCategories);
function getChildCategories($categories) {
$childCategories = [];
foreach ($categories as $category) {
if ($category->parent != 0) {
$childCategories[] = $category;
}
}
return $childCategories;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment