WordPress.org

Make WordPress Core

Ticket #12443: get_ancestors.2.12443.diff

File get_ancestors.2.12443.diff, 1.6 KB (added by filosofo, 8 years ago)
  • wp-includes/taxonomy.php

     
    26822682
    26832683        return false;
    26842684}
     2685 
     2686/**
     2687 * Get an array of ancestor IDs for a given object.
     2688 *
     2689 * @param int $object_id The ID of the object
     2690 * @param string $object_type The type of object for which we'll be retrieving ancestors.
     2691 * @return array of ancestors from lowest to highest in the hierarchy.
     2692 */
     2693function get_ancestors($object_id = 0, $object_type = '') {
     2694        $object_id = (int) $object_id;
     2695 
     2696        $ancestors = array();
     2697
     2698        if ( empty( $object_id ) ) {
     2699                return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
     2700        }
     2701               
     2702        if ( is_taxonomy_hierarchical( $object_type ) ) {
     2703                $term = get_term($object_id, $object_type);
     2704                while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
     2705                        $ancestors[] = (int) $term->parent;
     2706                        $term = get_term($term->parent, $object_type);
     2707                }
     2708        } elseif ( null !== get_post_type_object( $object_type ) ) {
     2709                $object = get_post($object_id);
     2710                if ( ! is_wp_error( $object ) && isset( $object->ancestors ) && is_array( $object->ancestors ) )
     2711                        $ancestors = $object->ancestors;
     2712                else {
     2713                        while ( ! is_wp_error($object) && ! empty( $object->post_parent ) && ! in_array( $object->post_parent, $ancestors ) ) {
     2714                                $ancestors[] = (int) $object->post_parent;
     2715                                $object = get_post($object->post_parent);
     2716                        }
     2717                }
     2718        }
     2719
     2720        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
     2721}