Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/url to postid


Retrieve id of page/post given its url, or zero on failure. Can be thought of as the opposite function of get_permalink().

When using this function to retrive the ID of an attachment, it is required that the url provided be in the format of example.com/?attachment_id=N and will not work with the full URL. To get the id from the full URL see this for a work around.

The function first checks for if the URL contains the ID of the item. If it doesn't it then does a in depth determination. However this requires Rewrite Rules to be enabled.

In a Plugin or Theme, it can be used as early as the setup_theme Action. Any earlier usage, including plugins_loaded, generates a Fatal Error.


 <?php $postid url_to_postid$url ); ?> 


(string) (required) The permalink to check.
Default: None

Return Values

ID of the post or page that resides at the given URL, or 0 on failure.

Change Log

  • 3.7.0: This function now supports custom post types (see Trac tickets #19744, #25659).
  • Since: 1.0.0

Source File

url_to_postid() is located in wp-includes/rewrite.php.