Ready to get started?Download WordPress


get_post_ancestors( $page->ID ) is always empty (3 posts)

  1. Sam_a
    Posted 4 years ago #

    I'm quite stumped. get_post_ancestors is supposed to accept an integer ID, but I get no ancestors unless I pass the page object itself:

    include '../wp-load.php';
    $pages =& get_pages();
    foreach ( $pages as $page ) {
    	var_dump( $page->ID // = string "integer"
    		, (bool) get_post( $page->ID ) // = bool true
    		, get_post_ancestors( $page->ID ) // = empty array
    		, get_post_ancestors( get_post( $page->ID ) ) // = empty array
    		, get_post_ancestors( $page ) ); // = array ancestors (success)
  2. Sam_a
    Posted 4 years ago #

    It's not actually the argument type (you can get missing ancestors with either an ID or object argument), but a problem with get_posts.

    Anyway, there's a patch now :-)

    Ticket #10381 (new defect (bug)): post->ancestors isn't always set

  3. Mats Mikkel Rummelhoff
    Posted 3 years ago #

    I just put this inside my search result loop to counter the bug:

    wp_cache_delete($post->ID, 'posts');

    Any drawbacks to this method?

Topic Closed

This topic has been closed to new replies.

About this Topic