wppaste
WordPress

attachment_url_to_postid( string $url ): int

Since
4.0.0
Source
wp-includes/media.php:5489
Tries to convert an attachment URL into a post ID.

Compatibility

WordPress
since 4.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
The URL to resolve.

Return value

int
The found post ID, or 0 on failure.

Performance profile

How much work a call to attachment_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_results().

Scaling
Scales with input

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

Instructions
12–80

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

Plugin surface
2 hooks

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

Called by
2

2 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
  • sqldatabase query->get_results()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

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

WhenInstructionsCalls it makes
$post_id !== null12apply_filters()
$post_id === null51–65apply_filters(), wp_get_upload_dir(), parse_url(), parse_url(), ->prepare(), ->get_results(), apply_filters()
$post_id === null58–80apply_filters(), wp_get_upload_dir(), parse_url(), parse_url(), ->prepare(), ->get_results(), reset(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8212–809
8.58212–809
8.48212–8099 fewer instructions than PHP 8.3
8.39112–899
8.29112–899
8.19112–899
7.49112–899

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 · 2

2 hooks fire while attachment_url_to_postid() runs, in this order:

  1. apply_filters( pre_attachment_url_to_postid )filterline 5514 (+25 into the body)

    Filters the attachment ID to allow short-circuit the function.

  2. apply_filters( attachment_url_to_postid )filterline 5564 (+75 into the body)

    Filters an attachment ID found by URL.

Uses · 3

Used by · 2

Source code

function attachment_url_to_postid( $url ) {	global $wpdb; 	/**	 * Filters the attachment ID to allow short-circuit the function.	 *	 * Allows plugins to short-circuit attachment ID lookups. Plugins making	 * use of this function should return:	 *	 * - 0 (integer) to indicate the attachment is not found,	 * - attachment ID (integer) to indicate the attachment ID found,	 * - null to indicate WordPress should proceed with the lookup.	 *	 * Warning: The post ID may be null or zero, both of which cast to a	 * boolean false. For information about casting to booleans see the	 * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}.	 * Use the === operator for testing the post ID when developing filters using	 * this hook.	 *	 * @since 6.7.0	 *	 * @param int|null $post_id The result of the post ID lookup. Null to indicate	 *                          no lookup has been attempted. Default null.	 * @param string   $url     The URL being looked up.	 */	$post_id = apply_filters( 'pre_attachment_url_to_postid', null, $url );	if ( null !== $post_id ) {		return (int) $post_id;	} 	$dir  = wp_get_upload_dir();	$path = $url; 	$site_url   = parse_url( $dir['url'] );	$image_path = parse_url( $path ); 	// Force the protocols to match if needed.	if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) {		$path = str_replace( $image_path['scheme'], $site_url['scheme'], $path );	} 	if ( str_starts_with( $path, $dir['baseurl'] . '/' ) ) {		$path = substr( $path, strlen( $dir['baseurl'] . '/' ) );	} 	$sql = $wpdb->prepare(		"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",		$path	); 	$results = $wpdb->get_results( $sql );	$post_id = null; 	if ( $results ) {		// Use the first available result, but prefer a case-sensitive match, if exists.		$post_id = reset( $results )->post_id; 		if ( count( $results ) > 1 ) {			foreach ( $results as $result ) {				if ( $path === $result->meta_value ) {					$post_id = $result->post_id;					break;				}			}		}	} 	/**	 * Filters an attachment ID found by URL.	 *	 * @since 4.2.0	 *	 * @param int|null $post_id The post_id (if any) found by the function.	 * @param string   $url     The URL being looked up.	 */	return (int) apply_filters( 'attachment_url_to_postid', $post_id, $url );}

Changelog

Introduced in 4.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.8.8 tag, from src/wp-includes/media.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.