public static function get_top_level_parent_id($post = null)
    {
        if (\is_search() || \is_404()) {
            return null;
        }

        switch ($post) {
            // No post has been set, so use global.
            case null:
                global $post;

            // The post ID has been provided.
            case \is_int($post):
                $post = \get_post($post);

            // A WP_Post was provided.
            case \is_object($post):
                $ancestors = $post->ancestors;
                break;

            case \is_array($post):
                $ancestors = $post['ancestors'];
                break;
        }

        if (isset($ancestors) && ! empty($ancestors)) {
            return (int) \end($ancestors);
        } else {
            return (int) $post->ID;
        }
    }
public static function get_top_level_parent_id($post = null)
    {
        if (\is_search() || \is_404()) {
            return null;
        }

        switch ($post) {
            // No post has been set, so use global.
            case null:
                global $post;

            // The post ID has been provided.
            case \is_int($post):
                $post = \get_post($post);

            // A WP_Post was provided.
            case \is_object($post):
                $ancestors = $post->ancestors;
                break;

            case \is_array($post):
                $ancestors = $post['ancestors'];
                break;
        }

        if (isset($ancestors) && ! empty($ancestors)) {
            return (int) \end($ancestors);
        } else {
            return (int) $post->ID;
        }
    }
public static function get_top_level_parent_id ( $ post = null ) { if ( \ is_search ( ) || \ is_404 ( ) ) { return null ; } switch ( $ post ) { // No post has been set, so use global. case null : global $ post ; // The post ID has been provided. case \ is_int ( $ post ) : $ post = \ get_post ( $ post ) ; // A WP_Post was provided. case \ is_object ( $ post ) : $ ancestors = $ post -> ancestors ; break ; case \ is_array ( $ post ) : $ ancestors = $ post [ 'ancestors' ] ; break ; } if ( isset ( $ ancestors ) && ! empty ( $ ancestors ) ) { return ( int ) \ end ( $ ancestors ) ; } else { return ( int ) $ post -> ID ; } }
Get value of top level hierarchical post ID.

Does not work with the objects returned by get_pages().

@since  1.0.0

@param (int|WP_Post|array) $post null Optional. Post object,array, or ID of a post to find the top ancestors for.
@return int ID
Get value of top level hierarchical post ID .