Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Edit:

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.

share|improve this question
    
Are you able to print anything in the else statement or is it just that $thisid isn't being recognized and therefore not executing get_post_ancestors()? –  developdaly Apr 6 '12 at 17:51
    
Please change the title: It should reflect the specific problem you have. –  toscho Apr 7 '12 at 20:33
    
@developdaly - As I mentioned, the whole thing works fine if I the post type is not 'ia_dir', and thus $thisid is not set by that if statement. So it appears that I'm not passing a valid format to get_post_ancestors - but I know absolutely that this post id is valid (the whole think works if I actually visit post 19, for example, but if I visit an 'ia_dir' post so that post 19 is manually passed to get_post_ancestors, the $ancestors array is empty. It exists, and the whole if statement works otherwise, but the array is empty.) –  jfacemyer Apr 10 '12 at 13:20
    
@toscho - sorry, I really didn't know what to put there because I wasn't sure what the problem was. I think it might have to do with an incorrect value being passed, but I'm still not certain. Is this better? –  jfacemyer Apr 10 '12 at 13:27
    
Yeah, better. I wasn't sure too; that's why I asked you to do it. :) When you write a title always think: How would I search for this problem? –  toscho Apr 10 '12 at 19:15
show 1 more comment

1 Answer 1

EDIT

$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.

share|improve this answer
    
Interesting. It does not say that at the Codex. Also, when $thisid comes from the post (instead of being manually set), it isn't an array. However, I'll try this out. –  jfacemyer Apr 10 '12 at 14:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.