• I’m in a page (NOT a post) and am trying to retrieve the Page ID using some embedded PHP in the page (using a PHP exec plugin). So far I’ve tried:

    <?php echo "pageid: ".$page->ID; ?>

    $page->ID returns empty, but using:
    $post->ID returns empty too,
    $get_the_ID() instead causes an error and stops execution,
    $the_ID() ditto above,
    $wp_query->post->ID returns empty, and finally:

    $this_page_id = get_query_var('page_id');
    echo "pageid: ".$this_page_id; ?>

    returns “pageid: 0”

    Any ideas what I could be doing wrong?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter MattParkins

    (@mattparkins)

    Solved. I hate wordpress.

    The solution was to use $post->ID as above, except IT NEEDS A global $post; first. Seriously, where is that in the docs?

    <?php echo “pageid: “.$post->ID; ?> // DOES NOT WORK

    <?php
    global $post;
    echo "pageid: ".$post->ID;
    ?>

    It is probably related to some php.ini setting, or PHP5 or something, but some kind of warning somewhere would have been nice.

    There is no difference in page or post id.
    Everything is a post, pages just have a different post_type.

    <?php the_ID(); ?> (inside the loop!)

    Thread Starter MattParkins

    (@mattparkins)

    Thanks for that, Rick, that helps me grasp what is going on.

    Unfortunately the_ID() did not work for me presumably because I wasn’t inside the loop, but in a single page. In fact the_ID() killed execution.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get current page ID (NOT post ID)’ is closed to new replies.