Title: attachment_url_to_postid
Published: September 4, 2014
Last modified: May 20, 2026

---

# attachment_url_to_postid( string $url ): int

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#wp--skip-link--target)

Tries to convert an attachment URL into a post ID.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#parameters)󠁿

 `$url`stringrequired

The URL to resolve.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#return)󠁿

 int The found post ID, or 0 on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#source)󠁿

    ```php
    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
    	 * 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 );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/media.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/media.php#L5499)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/media.php#L5499-L5575)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#hooks)󠁿

 [apply_filters( ‘attachment_url_to_postid’, int|null $post_id, string $url )](https://developer.wordpress.org/reference/hooks/attachment_url_to_postid/)

Filters an attachment ID found by URL.

 [apply_filters( ‘pre_attachment_url_to_postid’, int|null $post_id, string $url )](https://developer.wordpress.org/reference/hooks/pre_attachment_url_to_postid/)

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

## 󠀁[Related](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_get_upload_dir()](https://developer.wordpress.org/reference/functions/wp_get_upload_dir/)`wp-includes/functions.php` |

Retrieves uploads directory information.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [wpdb::get_results()](https://developer.wordpress.org/reference/classes/wpdb/get_results/)`wp-includes/class-wpdb.php` |

Retrieves an entire SQL result set from the database (i.e., many rows).

  | 
| [wpdb::prepare()](https://developer.wordpress.org/reference/classes/wpdb/prepare/)`wp-includes/class-wpdb.php` |

Prepares a SQL query for safe execution.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#)

| Used by | Description | 
| [wp_ajax_set_attachment_thumbnail()](https://developer.wordpress.org/reference/functions/wp_ajax_set_attachment_thumbnail/)`wp-admin/includes/ajax-actions.php` |

Handles setting the featured image for an attachment via AJAX.

  | 
| [WP_Customize_Upload_Control::to_json()](https://developer.wordpress.org/reference/classes/wp_customize_upload_control/to_json/)`wp-includes/customize/class-wp-customize-upload-control.php` |

Refresh the parameters passed to the JavaScript via JSON.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.0.0](https://developer.wordpress.org/reference/since/4.0.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#comment-content-1768)
 2.    [rabmalin](https://profiles.wordpress.org/rabmalin/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/#comment-1768)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fattachment_url_to_postid%2F%23comment-1768)
     Vote results for this note: 3[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fattachment_url_to_postid%2F%23comment-1768)
 4.  **Get post ID from attachment URL**
 5.      ```php
         echo attachment_url_to_postid( 'http://example.com/wp-content/uploads/2016/05/castle-old.jpg' );
         ```
     
 6.  Output:
 7.      ```php
         123
         ```
     
 8.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fattachment_url_to_postid%2F%3Freplytocom%3D1768%23feedback-editor-1768)
 9.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/?output_format=md#comment-content-6691)
 10.   [Milana Cap](https://profiles.wordpress.org/milana_cap/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/attachment_url_to_postid/#comment-6691)
 11. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fattachment_url_to_postid%2F%23comment-6691)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fattachment_url_to_postid%2F%23comment-6691)
 12. Applying WordPress coding standards and best practices, example should look like
     this, [Muhibul](https://profiles.wordpress.org/devmuhib/).
 13.     ```php
         $attachment_url     = 'https://example.com/wp-content/uploads/2023/09/image.jpg';
         $attachment_post_id = attachment_url_to_postid( esc_url( $attachment_url ) );
     
         if ( 0 !== $attachment_post_id ) {
             // An attachment post or page was found.
             printf( esc_html__( 'Attachment is associated with post ID: %s', 'textdomain'),
                 $attachment_post_id
             );
         } else {
             // No attachment post or page found for the URL.
             esc_html_e( 'No post or page found for the provided attachment URL.', 'textdomain');
         }
         ```
     
 14.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fattachment_url_to_postid%2F%3Freplytocom%3D6691%23feedback-editor-6691)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fattachment_url_to_postid%2F)
before being able to contribute a note or feedback.