0

I've seen plenty of tutorials on how to get the ID of the upper-most parent, but I can't seem to find anything on how to get the ID from the direct parent.

Eg;

- Big John
  - Little John
    - Baby John

How, if I'm on the Baby John page, can I get the ID of Little John and not of Big John?

2 Answers 2

6

I'm assuming that you're asking how to do it with code? (Doing it in backend is pretty easy - just try to edit parent page and see the ID in URL).

So how to check post parent in code? It's also very easy - direct parent is stored in post_parent property. If post has no parent, it will be 0.

So all you have to do is:

global $post;
$direct_parent = $post->post_parent;
1
  • I can confirm that this works. I think you should accept this as the best answer.
    – Robbiegod
    Commented Oct 17, 2020 at 3:19
2

$post->post_parent ?

http://codex.wordpress.org/Function_Reference/$post

1
  • 1
    A better reference page might be WP_Post.
    – Pat J
    Commented Nov 18, 2013 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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