url_to_postid

Examine a URL and try to determine the post ID it represents.

Description

(int) url_to_postid( (string) $url ); 

Checks are supposedly from the hosted site blog.

Returns (int)

Post ID, or 0 on failure.

Parameters (1)

0. $url (string)
Permalink to check.

Usage

  1. if ( !function_exists( 'url_to_postid' ) ) { 
  2. require_once ABSPATH . WPINC . '/rewrite.php'; 
  3.  
  4. // Permalink to check. 
  5. $url = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = url_to_postid($url); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/rewrite.php  
  1. function url_to_postid( $url ) { 
  2. global $wp_rewrite; 
  3.  
  4. /** 
  5. * Filters the URL to derive the post ID from. 
  6. * @since 2.2.0 
  7. * @param string $url The URL to derive the post ID from. 
  8. */ 
  9. $url = apply_filters( 'url_to_postid', $url ); 
  10.  
  11. // First, check to see if there is a 'p=N' or 'page_id=N' to match against 
  12. if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) ) { 
  13. $id = absint($values[2]); 
  14. if ( $id ) 
  15. return $id; 
  16.  
  17. // Get rid of the #anchor 
  18. $url_split = explode('#', $url); 
  19. $url = $url_split[0]; 
  20.  
  21. // Get rid of URL ?query=string 
  22. $url_split = explode('?', $url); 
  23. $url = $url_split[0]; 
  24.  
  25. // Set the correct URL scheme. 
  26. $scheme = parse_url( home_url(), PHP_URL_SCHEME ); 
  27. $url = set_url_scheme( $url, $scheme ); 
  28.  
  29. // Add 'www.' if it is absent and should be there 
  30. if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') ) 
  31. $url = str_replace('://', '://www.', $url); 
  32.  
  33. // Strip 'www.' if it is present and shouldn't be 
  34. if ( false === strpos(home_url(), '://www.') ) 
  35. $url = str_replace('://www.', '://', $url); 
  36.  
  37. if ( trim( $url, '/' ) === home_url() && 'page' == get_option( 'show_on_front' ) ) { 
  38. $page_on_front = get_option( 'page_on_front' ); 
  39.  
  40. if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) { 
  41. return (int) $page_on_front; 
  42.  
  43. // Check to see if we are using rewrite rules 
  44. $rewrite = $wp_rewrite->wp_rewrite_rules(); 
  45.  
  46. // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options 
  47. if ( empty($rewrite) ) 
  48. return 0; 
  49.  
  50. // Strip 'index.php/' if we're not using path info permalinks 
  51. if ( !$wp_rewrite->using_index_permalinks() ) 
  52. $url = str_replace( $wp_rewrite->index . '/', '', $url ); 
  53.  
  54. if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) { 
  55. // Chop off http://domain.com/[path] 
  56. $url = str_replace(home_url(), '', $url); 
  57. } else { 
  58. // Chop off /path/to/blog 
  59. $home_path = parse_url( home_url( '/' ) ); 
  60. $home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ; 
  61. $url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) ); 
  62.  
  63. // Trim leading and lagging slashes 
  64. $url = trim($url, '/'); 
  65.  
  66. $request = $url; 
  67. $post_type_query_vars = array(); 
  68.  
  69. foreach ( get_post_types( array() , 'objects' ) as $post_type => $t ) { 
  70. if ( ! empty( $t->query_var ) ) 
  71. $post_type_query_vars[ $t->query_var ] = $post_type; 
  72.  
  73. // Look for matches. 
  74. $request_match = $request; 
  75. foreach ( (array)$rewrite as $match => $query) { 
  76.  
  77. // If the requesting file is the anchor of the match, prepend it 
  78. // to the path info. 
  79. if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) ) 
  80. $request_match = $url . '/' . $request; 
  81.  
  82. if ( preg_match("#^$match#", $request_match, $matches) ) { 
  83.  
  84. if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) { 
  85. // This is a verbose page match, let's check to be sure about it. 
  86. $page = get_page_by_path( $matches[ $varmatch[1] ] ); 
  87. if ( ! $page ) { 
  88. continue; 
  89.  
  90. $post_status_obj = get_post_status_object( $page->post_status ); 
  91. if ( ! $post_status_obj->public && ! $post_status_obj->protected 
  92. && ! $post_status_obj->private && $post_status_obj->exclude_from_search ) { 
  93. continue; 
  94.  
  95. // Got a match. 
  96. // Trim the query of everything up to the '?'. 
  97. $query = preg_replace("!^.+\?!", '', $query); 
  98.  
  99. // Substitute the substring matches into the query. 
  100. $query = addslashes(WP_MatchesMapRegex::apply($query, $matches)); 
  101.  
  102. // Filter out non-public query vars 
  103. global $wp; 
  104. parse_str( $query, $query_vars ); 
  105. $query = array(); 
  106. foreach ( (array) $query_vars as $key => $value ) { 
  107. if ( in_array( $key, $wp->public_query_vars ) ) { 
  108. $query[$key] = $value; 
  109. if ( isset( $post_type_query_vars[$key] ) ) { 
  110. $query['post_type'] = $post_type_query_vars[$key]; 
  111. $query['name'] = $value; 
  112.  
  113. // Resolve conflicts between posts with numeric slugs and date archive queries. 
  114. $query = wp_resolve_numeric_slug_conflicts( $query ); 
  115.  
  116. // Do the query 
  117. $query = new WP_Query( $query ); 
  118. if ( ! empty( $query->posts ) && $query->is_singular ) 
  119. return $query->post->ID; 
  120. else 
  121. return 0; 
  122. return 0;