attachment_url_to_postid()
云策文档标注
概述
attachment_url_to_postid() 函数用于将附件 URL 转换为对应的文章 ID。它通过查询数据库中的 _wp_attached_file 元数据来实现,并支持过滤器钩子进行自定义处理。
关键要点
- 参数:接受一个字符串类型的 URL 参数,必需。
- 返回值:成功时返回附件对应的文章 ID(整数),失败时返回 0。
- 核心机制:基于上传目录路径解析 URL,查询 postmeta 表匹配 _wp_attached_file 元值。
- 过滤器钩子:提供 pre_attachment_url_to_postid 和 attachment_url_to_postid 钩子,允许插件干预查找过程。
- 注意事项:使用 === 运算符测试过滤器返回的 post ID,以避免类型转换问题。
代码示例
$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 ) {
// 找到附件文章或页面
printf( esc_html__( 'Attachment is associated with post ID: %s', 'textdomain' ),
$attachment_post_id
);
} else {
// 未找到对应附件
esc_html_e( 'No post or page found for the provided attachment URL.', 'textdomain' );
}
原文内容
Tries to convert an attachment URL into a post ID.
Parameters
$urlstringrequired-
The URL to resolve.
Source
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
* <a href="https://www.php.net/manual/en/language.types.boolean.php/">PHP documentation</a>.
* 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 );
}
Hooks
- apply_filters( ‘attachment_url_to_postid’, int|null $post_id, string $url )
-
Filters an attachment ID found by URL.
- apply_filters( ‘pre_attachment_url_to_postid’, int|null $post_id, string $url )
-
Filters the attachment ID to allow short-circuit the function.
Changelog
| Version | Description |
|---|---|
| 4.0.0 | Introduced. |
Skip to note 3 content
rabmalin
Get post ID from attachment URL
echo attachment_url_to_postid( 'http://example.com/wp-content/uploads/2016/05/castle-old.jpg' );Output:
Skip to note 4 content
Milana Cap
Applying WordPress coding standards and best practices, example should look like this, Muhibul.
$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'); }