Make WordPress Core

Changeset 15631


Ignore:
Timestamp:
09/18/2010 07:20:16 PM (14 years ago)
Author:
scribu
Message:

Introduce get_ancestors(). Props filosofo. Fixes #12443

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/taxonomy.php

    r15613 r15631  
    27762776    return false;
    27772777}
     2778 
     2779/**
     2780 * Get an array of ancestor IDs for a given object.
     2781 *
     2782 * @param int $object_id The ID of the object
     2783 * @param string $object_type The type of object for which we'll be retrieving ancestors.
     2784 * @return array of ancestors from lowest to highest in the hierarchy.
     2785 */
     2786function get_ancestors($object_id = 0, $object_type = '') {
     2787    $object_id = (int) $object_id;
     2788 
     2789    $ancestors = array();
     2790
     2791    if ( empty( $object_id ) ) {
     2792        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
     2793    }
     2794       
     2795    if ( is_taxonomy_hierarchical( $object_type ) ) {
     2796        $term = get_term($object_id, $object_type);
     2797        while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
     2798            $ancestors[] = (int) $term->parent;
     2799            $term = get_term($term->parent, $object_type);
     2800        }
     2801    } elseif ( null !== get_post_type_object( $object_type ) ) {
     2802        $object = get_post($object_id);
     2803        if ( ! is_wp_error( $object ) && isset( $object->ancestors ) && is_array( $object->ancestors ) )
     2804            $ancestors = $object->ancestors;
     2805        else {
     2806            while ( ! is_wp_error($object) && ! empty( $object->post_parent ) && ! in_array( $object->post_parent, $ancestors ) ) {
     2807                $ancestors[] = (int) $object->post_parent;
     2808                $object = get_post($object->post_parent);
     2809            }
     2810        }
     2811    }
     2812
     2813    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
     2814}
Note: See TracChangeset for help on using the changeset viewer.