I'm developing a Wordpress theme and I've registered a new menu location (called primary menu) and am using a menu with it. In my menu, I have a page called "tier-2 page" under a page called "tier-1 page".

I want to get the ID of the parent page ("tier-1 page") while on the child page ("tier-2 page"). I've tried $post->post_parent, get_post()->post_parent, get_post_ancestors($post->ID) and all of them return 0.

At this point, I have no idea why I can't retrieve the ID of the parent page. Any ideas?



kraftner is probably right but I recommand you to stop using global $post. I mean it's not bad but there are often issues with this big array that can be modified by almost everything running on the wp install.

So to me it's not very safe. I prefer using get_queried_object() and it works quite the same way :

$parent_ID = get_queried_object()->post_parent;

It retrieves data according to the current page. It uses the global $wp_query instead. Be careful here we try to get post_parent property so don't run this code on a category page you'd get a warning.

| improve this answer | |

I assume you haven't globalized $post before using it:

global $post

Have a look at the Codex if you need more detailed info.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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