Is there a good solution to get the queried object id really early ... and i mean really early?

I got a hook on after_setup_theme where i need to get the post or page id.

$wp_query->queried_object_id is null at that point.

I did it like so till now:

function getID() {

    $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $theID = url_to_postid($actual_link);
    if($theID == 0) $theID = get_option('page_on_front');

    return $theID;


Problem is that with WPML installed there are running some rewrite that seems to interfere with this solution cause example.com/foo is converted to example.com/en/foo

Any suggestions?

  • template_redirect is the safest I would say Commented May 8, 2015 at 12:11
  • Strange i thougt a had testet it on init and wp_loaded an it was to late ... on this hook it works just fine as far as is can see. Thank you.
    – Grandy
    Commented May 8, 2015 at 12:19

1 Answer 1


For mee it seems that the template_redirect hook worked. Thanks to Pieter Goosen.

Heres a solution that should work on earlier hooks:

function gdymc_object_exists( $object_id ) {

    return ( get_the_title( $object_id ) ) ? true : false;


function gdymc_objectID() {

    if( is_numeric( $_GET['page_id'] ) ):

        $object_id = $_GET['page_id'];

    elseif( is_numeric( $_GET['p'] ) ):

        $object_id = $_GET['p'];

    elseif( is_numeric( get_option( 'page_on_front' ) ) ):

        $object_id = get_option( 'page_on_front' );


        $object_id = 0;


    return gdymc_object_exists( $object_id) ? $object_id : false;


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.