get_post_ancestors example



            if ( empty( $terms ) ) {
                return array();
            }

            return wp_list_pluck( $terms, 'name' );
        }

        // Rest of the values need filtering.         if ( 'ancestors' === $key ) {
            $value = get_post_ancestors( $this );
        } else {
            $value = get_post_meta( $this->ID, $key, true );
        }

        if ( $this->filter ) {
            $value = sanitize_post_field( $key$value$this->ID, $this->filter );
        }

        return $value;
    }

    
$top_level_pages = array();

    $pages_with_children = array();

    $active_page_ancestor_ids = array();

    foreach ( (array) $all_pages as $page ) {
        $is_active = ! empty( $page->ID ) && ( get_queried_object_id() === $page->ID );

        if ( $is_active ) {
            $active_page_ancestor_ids = get_post_ancestors( $page->ID );
        }

        if ( $page->post_parent ) {
            $pages_with_children[ $page->post_parent ][ $page->ID ] = array(
                'page_id'   => $page->ID,
                'title'     => $page->post_title,
                'link'      => get_permalink( $page ),
                'is_active' => $is_active,
            );
        } else {
            $top_level_pages[ $page->ID ] = array(
                
setup_postdata( $post );

        $classes = 'iedit author-' . ( get_current_user_id() === (int) $post->post_author ? 'self' : 'other' );

        $lock_holder = wp_check_post_lock( $post->ID );

        if ( $lock_holder ) {
            $classes .= ' wp-locked';
        }

        if ( $post->post_parent ) {
            $count    = count( get_post_ancestors( $post->ID ) );
            $classes .= ' level-' . $count;
        } else {
            $classes .= ' level-0';
        }
        ?> <tr id="post-<?php echo $post->ID; ?>" class="<?php echo implode( ' ', get_post_class( $classes$post->ID ) ); ?>"> <?php $this->single_row_columns( $post ); ?> </tr> <?php         $GLOBALS['post'] = $global_post;
    }

    
$resource_type = 'post_type';
        }
    }

    if ( 'taxonomy' === $resource_type ) {
        $term = get_term( $object_id$object_type );
        while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors, true ) ) {
            $ancestors[] = (int) $term->parent;
            $term        = get_term( $term->parent, $object_type );
        }
    } elseif ( 'post_type' === $resource_type ) {
        $ancestors = get_post_ancestors( $object_id );
    }

    /** * Filters a given object's ancestors. * * @since 3.1.0 * @since 4.1.1 Introduced the `$resource_type` parameter. * * @param int[] $ancestors An array of IDs of object ancestors. * @param int $object_id Object ID. * @param string $object_type Type of object. * @param string $resource_type Type of resource $object_type is. */
Home | Imprint | This part of the site doesn't use cookies.