WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] New error notice for WP_Post::$ancestors in 3.5 (9 posts)

  1. petherika
    Member
    Posted 1 year ago #

    I have used this function without problems in the past, but in 3.5, I have received an error notice, which reads:

    Notice: Indirect modification of overloaded property WP_Post::$ancestors has no effect in /mysite.com/public_html/wp-content/themes/mytheme/functions.php on line 09

    Line 9 is:
    return end($post->ancestors);

    function get_top_parent_page_id() {
    
        global $post;
    
        // Check if page is a child page (any level)
        if ($post->ancestors) {
    
            //  Grab the ID of top-level page from the tree
            return end($post->ancestors);
    
        } else {
    
            // Page is the top level, so use  it's own id
            return $post->ID;
    
        }
    
    }

    The function I have found at:
    http://www.webcitizenmag.com/2010/05/20/how-to-get-top-parent-page-id-in-wordpress/

    Is this a bug in wordpress?
    Or is there something that changed in the core code?

    The function still works properly if I do not have debugg set to true. But it would be good to get rid of it.

    This guy has a similar problem in WP 3.5. Link.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try replacing:

    return end($post->ancestors);

    with:

    return end( get_post_ancestors($post) );

  3. petherika
    Member
    Posted 1 year ago #

    Yes it worked. Thank you.
    But why?

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    TBH, I'm not quite sure. I think the previous code was conflicting with a pre-loaded WordPress core class.

  5. petherika
    Member
    Posted 1 year ago #

    Okay, thank you.

    However, I am still a little curious as to why it worked earlier before 3.5.

  6. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    There have been quite a few changes to WordPress core and I can only guess that one of these changes caused the warning.

  7. nikolov.tmw
    Member
    Posted 1 year ago #

    I think that this is due to the fact, that the ancestors property is no-longer loaded by default(although I'm not sure if it was before 3.5).

    The thing is that when you request the ancestors property, PHP sees that it's not a property that has been defined for that WP_Post object. So PHP tries to call the __get() method of the class(which in the case of WP_Post exists). This is a "magical" method in PHP and is used to autoload properties for an object.

    Can you try with this code:

    function get_top_parent_page_id() {
    
        global $post;
    
        $ancestors = $post->ancestors;
    
        // Check if page is a child page (any level)
        if ($ancestors) {
    
            //  Grab the ID of top-level page from the tree
            return end($ancestors);
    
        } else {
    
            // Page is the top level, so use  it's own id
            return $post->ID;
    
        }
    
    }
  8. petherika
    Member
    Posted 1 year ago #

    Yes, the update function works great.
    Nice and simple. Many thanks.

    Also thanks for the explanation.

  9. Sam_a
    Member
    Posted 11 months ago #

    "Post objects are now instances of a WP_Post class" since http://codex.wordpress.org/Version_3.5 . They used to be just standard objects.

Topic Closed

This topic has been closed to new replies.

About this Topic