attachment_url_to_postid

The timeline below displays how wordpress function attachment_url_to_postid has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Tries to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID, or 0 on failure.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $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);
}

WordPress Version: 5.5

/**
 * Tries to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID, or 0 on failure.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $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 (0 === strpos($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);
}

WordPress Version: 5.4

/**
 * Tries to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID, or 0 on failure.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $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 (0 === strpos($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);
}

WordPress Version: 3.3

/**
 * Tries to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID, or 0 on failure.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $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 (0 === strpos($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);
}

WordPress Version: 3.2

/**
 * Tries to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID, or 0 on failure.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $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 (0 === strpos($path, $dir['baseurl'] . '/')) {
        $path = substr($path, strlen($dir['baseurl'] . '/'));
    }
    $sql = $wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path);
    $post_id = $wpdb->get_var($sql);
    /**
     * 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);
}

WordPress Version: .10

/**
 * Tries to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID, or 0 on failure.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $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 (0 === strpos($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);
}

WordPress Version: 4.6

/**
 * Tries to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID, or 0 on failure.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $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 (0 === strpos($path, $dir['baseurl'] . '/')) {
        $path = substr($path, strlen($dir['baseurl'] . '/'));
    }
    $sql = $wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path);
    $post_id = $wpdb->get_var($sql);
    /**
     * 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);
}

WordPress Version: 4.5

/**
 * Tries to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID, or 0 on failure.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $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 (0 === strpos($path, $dir['baseurl'] . '/')) {
        $path = substr($path, strlen($dir['baseurl'] . '/'));
    }
    $sql = $wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path);
    $post_id = $wpdb->get_var($sql);
    /**
     * Filter 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);
}

WordPress Version: 4.4

/**
 * Tries to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID, or 0 on failure.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $dir = wp_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 (0 === strpos($path, $dir['baseurl'] . '/')) {
        $path = substr($path, strlen($dir['baseurl'] . '/'));
    }
    $sql = $wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path);
    $post_id = $wpdb->get_var($sql);
    /**
     * Filter 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);
}

WordPress Version: 4.2

/**
 * Tries to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID, or 0 on failure.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $dir = wp_upload_dir();
    $path = $url;
    if (0 === strpos($path, $dir['baseurl'] . '/')) {
        $path = substr($path, strlen($dir['baseurl'] . '/'));
    }
    $sql = $wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path);
    $post_id = $wpdb->get_var($sql);
    /**
     * Filter 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.
     */
    $post_id = apply_filters('attachment_url_to_postid', $post_id, $url);
    return (int) $post_id;
}

WordPress Version: 4.1

/**
 * Try to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $dir = wp_upload_dir();
    $path = $url;
    if (0 === strpos($path, $dir['baseurl'] . '/')) {
        $path = substr($path, strlen($dir['baseurl'] . '/'));
    }
    $sql = $wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path);
    $post_id = $wpdb->get_var($sql);
    if (!empty($post_id)) {
        return (int) $post_id;
    }
}

WordPress Version: 4.0

/**
 * Try to convert an attachment URL into a post ID.
 *
 * @since 4.0.0
 *
 * @global wpdb $wpdb WordPress database access abstraction object.
 *
 * @param string $url The URL to resolve.
 * @return int The found post ID.
 */
function attachment_url_to_postid($url)
{
    global $wpdb;
    $dir = wp_upload_dir();
    $path = ltrim($url, $dir['baseurl'] . '/');
    $sql = $wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path);
    $post_id = $wpdb->get_var($sql);
    if (!empty($post_id)) {
        return (int) $post_id;
    }
}