Skip to content

Instantly share code, notes, and snippets.

@ivandoric
Created April 23, 2014 13:35
Show Gist options
  • Save ivandoric/11215420 to your computer and use it in GitHub Desktop.
Save ivandoric/11215420 to your computer and use it in GitHub Desktop.
wordpress: Get last child category ID
<?php
//Get all terms associated with post in taxonomy 'category'
$terms = get_the_terms(get_the_ID(),'category');
//Get an array of their IDs
$term_ids = wp_list_pluck($terms,'term_id');
//Get array of parents - 0 is not a parent
$parents = array_filter(wp_list_pluck($terms,'parent'));
//Get array of IDs of terms which are not parents.
$term_ids_not_parents = array_diff($term_ids, $parents);
//Get corresponding term objects
$terms_not_parents = array_intersect_key($terms, $term_ids_not_parents);
//print_r($terms_not_parents);
echo key($terms_not_parents);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment