wppaste
WordPress

url_to_postid( string $url ): int

Since
1.0.0
Source
wp-includes/rewrite.php:490
Examines a URL and try to determine the post ID it represents.

Description

Checks are supposedly from the hosted site blog.

Compatibility

WordPress
since 1.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$urlstring
Permalink to check.

Return value

int
Post ID, or 0 on failure.

Performance profile

How much work a call to url_to_postid() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
77–222

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 296.

Plugin surface
1 hook

Third-party callbacks on 'url_to_postid' run inside this call, and their cost is not bounded by anything here.

Called by
7

7 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()called directly
  • querycontent queryget_post()called directly
  • cacheobject cachewp_cache_get()one call below url_to_postid()
  • serializeserialisationmaybe_unserialize()one call below url_to_postid()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 22 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost url_to_postid() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && empty($rewrite)77apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules()
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && empty($rewrite)82apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules()
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && !empty($rewrite)108–115apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types()
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && !empty($rewrite)113–120apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types()
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && !empty($rewrite)125–133apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types()
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && !empty($rewrite)130–138apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types()
!is_string($url_host) && !is_string($home_url_host) && !preg_match() && !empty($rewrite) && ->using_index_permalinks()135–138apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && !$wp_rewrite153–171apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && !$wp_rewrite158–176apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite159–177apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite164–182apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && !$wp_rewrite170–189apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
10 further outcomes, up to 221 instructions
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite175–199apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), preg_match(), get_page_by_path(), get_post_status_object(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && !$wp_rewrite175–194apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite176–195apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite180–204apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), get_post_types(), preg_match(), preg_match(), get_page_by_path(), get_post_status_object(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && ->using_index_permalinks() && !$rewrite && !$wp_rewrite180–193apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite181–200apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && ->using_index_permalinks() && !$rewrite && $wp_rewrite186–199apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite192–217apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), get_page_by_path(), get_post_status_object(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && !$rewrite && $wp_rewrite197–222apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), get_page_by_path(), get_post_status_object(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()
!is_string($url_host) && !is_string($home_url_host) && !empty($rewrite) && ->using_index_permalinks() && !$rewrite && $wp_rewrite202–221apply_filters(), parse_url(), home_url(), parse_url(), preg_match(), explode(), explode(), home_url(), parse_url(), set_url_scheme(), home_url(), home_url(), home_url(), get_option(), get_option(), ->wp_rewrite_rules(), ->using_index_permalinks(), trailingslashit(), home_url(), home_url(), parse_url(), preg_quote(), trailingslashit(), get_post_types(), preg_match(), preg_match(), get_page_by_path(), get_post_status_object(), ::WP_MatchesMapRegex(), addslashes(), parse_str(), wp_resolve_numeric_slug_conflicts()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev29677–22239
8.529677–22239
8.429677–2223949 fewer instructions than PHP 8.3
8.334586–25039
8.234586–25039
8.134586–250391 fewer instruction than PHP 7.4
7.434686–25139

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while url_to_postid() runs, in this order:

  1. apply_filters( url_to_postid )filterline 500 (+10 into the body)

    Filters the URL to derive the post ID from.

Uses · 15

Show all 15

Used by · 7

Source code

function url_to_postid( $url ) {	global $wp_rewrite; 	/**	 * Filters the URL to derive the post ID from.	 *	 * @since 2.2.0	 *	 * @param string $url The URL to derive the post ID from.	 */	$url = apply_filters( 'url_to_postid', $url ); 	$url_host = parse_url( $url, PHP_URL_HOST ); 	if ( is_string( $url_host ) ) {		$url_host = str_replace( 'www.', '', $url_host );	} else {		$url_host = '';	} 	$home_url_host = parse_url( home_url(), PHP_URL_HOST ); 	if ( is_string( $home_url_host ) ) {		$home_url_host = str_replace( 'www.', '', $home_url_host );	} else {		$home_url_host = '';	} 	// Bail early if the URL does not belong to this site.	if ( $url_host && $url_host !== $home_url_host ) {		return 0;	} 	// First, check to see if there is a 'p=N' or 'page_id=N' to match against.	if ( preg_match( '#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values ) ) {		$id = absint( $values[2] );		if ( $id ) {			return $id;		}	} 	// Get rid of the #anchor.	$url_split = explode( '#', $url );	$url       = $url_split[0]; 	// Get rid of URL ?query=string.	$url_split = explode( '?', $url );	$url       = $url_split[0]; 	// Set the correct URL scheme.	$scheme = parse_url( home_url(), PHP_URL_SCHEME );	$url    = set_url_scheme( $url, $scheme ); 	// Add 'www.' if it is absent and should be there.	if ( str_contains( home_url(), '://www.' ) && ! str_contains( $url, '://www.' ) ) {		$url = str_replace( '://', '://www.', $url );	} 	// Strip 'www.' if it is present and shouldn't be.	if ( ! str_contains( home_url(), '://www.' ) ) {		$url = str_replace( '://www.', '://', $url );	} 	if ( trim( $url, '/' ) === home_url() && 'page' === get_option( 'show_on_front' ) ) {		$page_on_front = get_option( 'page_on_front' ); 		if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {			return (int) $page_on_front;		}	} 	// Check to see if we are using rewrite rules.	$rewrite = $wp_rewrite->wp_rewrite_rules(); 	// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options.	if ( empty( $rewrite ) ) {		return 0;	} 	// Strip 'index.php/' if we're not using path info permalinks.

Changelog

Introduced in 1.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/rewrite.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.