admin管理员组文章数量:1317906
We have about 2000 files in our media library and we are working on an audit to organize the files and create a name hierarchy.
But until that is done, I am trying to locate a handful of specific files to replace. I don't know what the files are named in Wordpress, I only have the URL to download the file.
Is there a way to search for the file in the dashboard using the URL?
We have about 2000 files in our media library and we are working on an audit to organize the files and create a name hierarchy.
But until that is done, I am trying to locate a handful of specific files to replace. I don't know what the files are named in Wordpress, I only have the URL to download the file.
Is there a way to search for the file in the dashboard using the URL?
Share Improve this question asked Oct 28, 2020 at 20:09 Garrett MasseyGarrett Massey 232 bronze badges 1- If you have the url to download, then you have the file name. Your url should be something like yourdomain/wp-content/uploads/2020/08/filename-150x150.jpg – Faye Commented Oct 28, 2020 at 23:29
1 Answer
Reset to default 0Based on my research, you would need to run a query on directly on the database using WPDB. So you would do something like this:
Add this in your functions.php
file:
// retrieves the attachment ID from the file URL
function get_image_id($image_url) {
global $wpdb;
$the_attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $the_attachment[0];
}
And then you can apply it wherever you like this way:
$attachment_url = 'http://example/wp-content/uploads/2020/10/28/just_for_the_lolz.jpg';
$attachment_id = get_image_id($image_url);
$attachment_id
will have the attachment ID so you can do whatever you want with it.
本文标签: wp filesystemHow do you find a file in the media library using the file URL
版权声明:本文标题:wp filesystem - How do you find a file in the media library using the file URL? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742030288a2416315.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论