WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an URL (such as the URL /sample-page/), I need the corresponding object ID. How?

share|improve this question

Try this function:

url_to_postid( $url );

The WordPress codex is your friend. A quick Google search could have provided the answer.

share|improve this answer

Use get_queried_object_id() a mapper for WP_Query::get_queried_object_id().

Unlike url_to_postid() you don't have to know the URL and it works on taxonomies and custom post types too (if I remember this correct).

For a usage example see my answer to Get current term's ID.

share|improve this answer

The other answers are valid, but just to add another (usually unknown, but very usefull ) method that is supplied by a function called :get_page_by_path();

$page =get_page_by_path('parent-page/sub-page');

It is worthy to note that the path parameter can even take a SLUG as parameter. Example :

$page = get_page_by_path('your-slug-of-page-to-exclude');

then you could use $page->ID

share|improve this answer
Doesn't work, $page-ID returns error Notice: Trying to get property of non-object in .... – Murhaf Sousli Feb 18 '15 at 1:58
did you tried $page->ID or $page-ID ? – krembo99 Jun 28 '15 at 14:27

I had the same problem, only with custom post types. Unfortunately url_to_postid( $url ); is not working with custom types, so I ended up using this function bwp_url_to_postid($url). This function extend url_to_postid( $url ); to work with custom post types.

If you have the permalink of the URL, then:

<?php $postLink = get_permalink($post_object->ID); ?>
<?php $postId = bwp_url_to_postid($link); ?>
<?php echo $postId ?>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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