WordPress.org

Make WordPress Core

Ticket #12443: get_ancestors.12443.diff

File get_ancestors.12443.diff, 1.3 KB (added by filosofo, 4 years ago)
  • wp-includes/taxonomy.php

     
    25522552        return false; 
    25532553} 
    25542554 
     2555/** 
     2556 * Get an array of ancestor IDs for a given object. 
     2557 * 
     2558 * @param int $object_id The ID of the object 
     2559 * @param string $object_type The type of object for which we'll be retrieving ancestors. 
     2560 * @return array of ancestors from lowest to highest in the hierarchy. 
     2561 */ 
     2562function get_ancestors($object_id = 0, $object_type = '') { 
     2563        $object_id = (int) $object_id; 
    25552564 
     2565        $ancestors = array(); 
     2566 
     2567        if ( empty( $object_id ) ) { 
     2568                return apply_filters('get_ancestors', $ancestors, $object_id, $object_type); 
     2569        } 
     2570                 
     2571        if ( is_taxonomy_hierarchical( $object_type ) ) { 
     2572                $term = get_term($object_id, $object_type); 
     2573                while ( ! is_wp_error($term) && ! empty( $term->parent ) ) { 
     2574                        $ancestors[] = (int) $term->parent; 
     2575                        $term = get_term($term->parent, $object_type); 
     2576                } 
     2577        } elseif ( null !== get_post_type_object( $object_type ) ) { 
     2578                $object = get_post($object_id); 
     2579                while ( ! is_wp_error($object) && ! empty( $object->post_parent ) ) { 
     2580                        $ancestors[] = (int) $object->post_parent; 
     2581                        $object = get_post($object->post_parent); 
     2582                } 
     2583        } 
     2584 
     2585        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type); 
     2586} 
    25562587?>