What about using the WordPress function get_ancestors()
, which
Returns an array containing the parents of the given object.
To be exact an
Array of ancestors from lowest to highest in the hierarchy
We can easily create a function to return a list by using implode
to do so:
function wpse185971_get_ancestors_list(
$object_id,
$object_type="category",
$separator=","
) {
$ancestors_array = get_ancestors( $object_id, $object_type );
$ancestors_list = implode( $separator, $ancestors_array );
return $ancestors_list;
}