[wp-hackers] problem with $post->post_parent

Brian Fidler fidler.brian at gmail.com
Tue Mar 30 01:10:49 UTC 2010


As far as I know the page hierarchy is set up correctly. In WP's admin view
my Shop Page has a number of children and when editing them they show Shop
as their parent page.

When I am on the Shop page and echo out the page ID I get the correct post
ID which is 29, however if I go to one of the Shop page's children it won't
fetch the Parent ID at all. This holds true for each of my main level pages
(pages w/o parents).

I tested with this code :

global $post;

function checkPostID() {
    global $wp_query;
    $thePostID = $wp_query->post->ID;
    $myParent = $post->post_parent;
    echo "i am post ". $thePostID . " and my parent is " . $myParent;
}


if ( is_page('paintings')) {
    checkPostID();
   $myGallery='3';

} elseif ( is_page('loteria')) {
    checkPostID();
   $myGallery='2';

} elseif ( is_page('prints')) {
    checkPostID();
   $myGallery='18';

} elseif ( is_page('public-art')) {
   checkPostID();
   $myGallery='18';

} elseif ( is_page('shop')) {
    checkPostID();
    $myGallery='4';

} else {
    checkPostID();
    echo "and I don't know my parent!";
    $myGallery='2'; // just in case we are at an unclassified page, perhaps
the home page
}

?>





[image: brian fidler interactive] <http://www.brianfidler.com>
email: interactive at brianfidler.com
phone: 602.758.4733
[image: View Brian Fidler's profile on
LinkedIn]<http://www.linkedin.com/in/brianfidler>


On Sun, Mar 28, 2010 at 9:58 PM, Will Anderson <will at itsananderson.com>wrote:

> Perhaps a silly question, but you've actually set up the page hierarchy
> correctly, right?
>
> Also, depending on when this code is running, $post may not have been
> populated yet. If you're trying to do this from a plugin you may need to do
> some hook foo.
>
> A judiciously placed  print_r of the $post variable should clear up both of
> these questions.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list