get_ancestors

Definition:
function get_ancestors($object_id = 0, $object_type = '') {}

Get an array of ancestor IDs for a given object.

Parameters

  • int $object_id: The ID of the object
  • string $object_type: The type of object for which we’ll be retrieving ancestors.

Return values

returns:of ancestors from lowest to highest in the hierarchy.

Defined filters

  • get_ancestors
    apply_filters('get_ancestors', $ancestors, $object_id, $object_type)
  • get_ancestors
    apply_filters('get_ancestors', $ancestors, $object_id, $object_type)

Source code

function get_ancestors($object_id = 0, $object_type = '') {

	$object_id = (int) $object_id;



	$ancestors = array();



	if ( empty( $object_id ) ) {

		return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);

	}



	if ( is_taxonomy_hierarchical( $object_type ) ) {

		$term = get_term($object_id, $object_type);

		while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {

			$ancestors[] = (int) $term->parent;

			$term = get_term($term->parent, $object_type);

		}

	} elseif ( null !== get_post_type_object( $object_type ) ) {

		$object = get_post($object_id);

		if ( ! is_wp_error( $object ) && isset( $object->ancestors ) && is_array( $object->ancestors ) )

			$ancestors = $object->ancestors;

		else {

			while ( ! is_wp_error($object) && ! empty( $object->post_parent ) && ! in_array( $object->post_parent, $ancestors ) ) {

				$ancestors[] = (int) $object->post_parent;

				$object = get_post($object->post_parent);

			}

		}

	}



	return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);

}

9341

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: