I have an URL (such as the URL /sample-page/
), I need the corresponding object ID. How?
Leave a Reply
You must be logged in to post a comment.
I have an URL (such as the URL /sample-page/
), I need the corresponding object ID. How?
You must be logged in to post a comment.
Try this function:
The WordPress codex is your friend. A quick Google search could have provided the 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();
It is worthy to note that the path parameter can even take a SLUG as parameter.
Example :
then you could use
$page->ID
Use
get_queried_object_id()
a mapper forWP_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.
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: