get_post_ancestors

The timeline below displays how wordpress function get_post_ancestors has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.6

/**
 * Retrieves the IDs of the ancestors of a post.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Post ID or post object.
 * @return int[] Array of ancestor IDs or empty array if there are none.
 */
function get_post_ancestors($post)
{
    $post = get_post($post);
    if (!$post || empty($post->post_parent) || $post->post_parent == $post->ID) {
        return array();
    }
    $ancestors = array();
    $id = $post->post_parent;
    $ancestors[] = $id;
    while ($ancestor = get_post($id)) {
        // Loop detection: If the ancestor has been seen before, break.
        if (empty($ancestor->post_parent) || $ancestor->post_parent == $post->ID || in_array($ancestor->post_parent, $ancestors, true)) {
            break;
        }
        $id = $ancestor->post_parent;
        $ancestors[] = $id;
    }
    return $ancestors;
}

WordPress Version: 5.5

/**
 * Retrieve ancestors of a post.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Post ID or post object.
 * @return int[] Ancestor IDs or empty array if none are found.
 */
function get_post_ancestors($post)
{
    $post = get_post($post);
    if (!$post || empty($post->post_parent) || $post->post_parent == $post->ID) {
        return array();
    }
    $ancestors = array();
    $id = $post->post_parent;
    $ancestors[] = $id;
    while ($ancestor = get_post($id)) {
        // Loop detection: If the ancestor has been seen before, break.
        if (empty($ancestor->post_parent) || $ancestor->post_parent == $post->ID || in_array($ancestor->post_parent, $ancestors, true)) {
            break;
        }
        $id = $ancestor->post_parent;
        $ancestors[] = $id;
    }
    return $ancestors;
}

WordPress Version: 5.4

/**
 * Retrieve ancestors of a post.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Post ID or post object.
 * @return int[] Ancestor IDs or empty array if none are found.
 */
function get_post_ancestors($post)
{
    $post = get_post($post);
    if (!$post || empty($post->post_parent) || $post->post_parent == $post->ID) {
        return array();
    }
    $ancestors = array();
    $id = $post->post_parent;
    $ancestors[] = $id;
    while ($ancestor = get_post($id)) {
        // Loop detection: If the ancestor has been seen before, break.
        if (empty($ancestor->post_parent) || $ancestor->post_parent == $post->ID || in_array($ancestor->post_parent, $ancestors)) {
            break;
        }
        $id = $ancestor->post_parent;
        $ancestors[] = $id;
    }
    return $ancestors;
}

WordPress Version: 5.3

/**
 * Retrieve ancestors of a post.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Post ID or post object.
 * @return array Ancestor IDs or empty array if none are found.
 */
function get_post_ancestors($post)
{
    $post = get_post($post);
    if (!$post || empty($post->post_parent) || $post->post_parent == $post->ID) {
        return array();
    }
    $ancestors = array();
    $id = $post->post_parent;
    $ancestors[] = $id;
    while ($ancestor = get_post($id)) {
        // Loop detection: If the ancestor has been seen before, break.
        if (empty($ancestor->post_parent) || $ancestor->post_parent == $post->ID || in_array($ancestor->post_parent, $ancestors)) {
            break;
        }
        $id = $ancestor->post_parent;
        $ancestors[] = $id;
    }
    return $ancestors;
}

WordPress Version: 3.9

/**
 * Retrieve ancestors of a post.
 *
 * @since 2.5.0
 *
 * @param int|WP_Post $post Post ID or post object.
 * @return array Ancestor IDs or empty array if none are found.
 */
function get_post_ancestors($post)
{
    $post = get_post($post);
    if (!$post || empty($post->post_parent) || $post->post_parent == $post->ID) {
        return array();
    }
    $ancestors = array();
    $id = $ancestors[] = $post->post_parent;
    while ($ancestor = get_post($id)) {
        // Loop detection: If the ancestor has been seen before, break.
        if (empty($ancestor->post_parent) || $ancestor->post_parent == $post->ID || in_array($ancestor->post_parent, $ancestors)) {
            break;
        }
        $id = $ancestors[] = $ancestor->post_parent;
    }
    return $ancestors;
}

WordPress Version: 3.7

/**
 * Retrieve ancestors of a post.
 *
 * @since 2.5.0
 *
 * @param int|object $post Post ID or post object
 * @return array Ancestor IDs or empty array if none are found.
 */
function get_post_ancestors($post)
{
    $post = get_post($post);
    if (!$post || empty($post->post_parent) || $post->post_parent == $post->ID) {
        return array();
    }
    $ancestors = array();
    $id = $ancestors[] = $post->post_parent;
    while ($ancestor = get_post($id)) {
        // Loop detection: If the ancestor has been seen before, break.
        if (empty($ancestor->post_parent) || $ancestor->post_parent == $post->ID || in_array($ancestor->post_parent, $ancestors)) {
            break;
        }
        $id = $ancestors[] = $ancestor->post_parent;
    }
    return $ancestors;
}