6:55 - Wednesday, 16 April 2014

Not Passing Correct Value To Get_post_ancestors?

I’ve got a custom plugin that creates a menu in the sidebar.

I need to be able to specify that, if a particular custom post type is current, the menu displays as if it were on a specific page (so that it looks as if the custom post is actually a child of the specified page.)

Here’s my code:

            global $wp-query;            $thisid = $wp_query->post->ID;            $type = get_post_type( $thisid );            if($type == 'ia_dir'){                $thisid = '19';                }            $thispost = get_post($thisid);                if (!$thispost->post_parent) {                    // if top-level, display only the subpages of this top level page                    $children = wp_list_pages("title_li=&sort_column=menu_order&child_of=".$thisid."&echo=0&depth=1");                    if($children) { // Get post ID to display section title only if this top level page has children                        $top_page = $thisid;                    }                }else{ // if not, display the siblings of all ancestors and the siblings and children of the current post                    $ancestors = get_post_ancestors($thisid);                        // now you can get the ID of the top ancestor of this page;  wp is putting the ids DESC, thats why the top level ID is the last one, and that's why we use "end"                        $top_page = end($ancestors);                        //initialize pagelist variable to hold list of pages to include                        $pagelist = "";                        //add the immediate children (no grandchildren) of each page in this page's ancestory to the list                        foreach ($ancestors as $ancestor) {                            $pageset = get_posts('numberposts=-1&post_type=page&post_parent='.$ancestor);                            foreach ($pageset as $apage) {                                $pagelist = $pagelist.$apage->ID.",";                                }                            }                        //add any children of the current page to the list                        $pageset = get_posts('numberposts=-1&post_type=page&post_parent='.$thisid);                        foreach ($pageset as $apage) {                            $pagelist = $pagelist.$apage->ID.",";                            }                        //get the list of pages, including only those in our page list                        $children = wp_list_pages('title_li=&echo=0&sort_column=menu_order&include='.$pagelist);                    }

The Issue I’m having is in the “else” section of the “post parent” check. If the post is not specified as “ia_dir” post type, then everything works well, I can get the ancestors and create the list.

If it is an “ia_dir” post, however, (i.e., the id is passed manually to $thisid and then to the get_post_ancestors), then I get an empty $ancestors array. I’ve tried a bunch of different page ids in the type check, and nothing works. I can even specify a page id that I know works fine if I actually visit that page, but when I go to an “ia_dir” post and pass that page id manually, the array is empty.

This is really odd. The only thing I’m doing differently is passing a specific manual page id number to $thisid. Why is this not working?


The else statement is functioning either way – I get the $ancestors array always. If I visit post 19 (which is a page), I get the listing because the $ancestors array is populated. However, if I visit an ‘ia_dir’ post, I get an empty $ancestors array, as if I did not pass ’19′ to get post ancestors.

So I assume I’m passing an incorrect value to get_post_ancestors. But after poring over the WP codex, I can’t figure out why passing the post number, either by a string or as as an integer, won’t work for me.


$thisid can be passed as an array, as get_post_anscestors().

So try $ancestors = get_post_ancestors( array( $thisid ) );

I misread; it does accept strings, but maybe give an array a shot.