<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: Verifying Existing Attachments in the Database	</title>
	<atom:link href="https://krasenslavov.com/verifying-existing-attachments-in-the-database/feed/" rel="self" type="application/rss+xml" />
	<link>https://krasenslavov.com/verifying-existing-attachments-in-the-database/</link>
	<description>WordPress Plugins, Themes and Writings</description>
	<lastBuildDate>Wed, 03 Dec 2025 03:58:20 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>
		By: Krasen Slavov		</title>
		<link>https://krasenslavov.com/verifying-existing-attachments-in-the-database/#comment-9390</link>

		<dc:creator><![CDATA[Krasen Slavov]]></dc:creator>
		<pubDate>Tue, 20 Feb 2024 10:07:19 +0000</pubDate>
		<guid isPermaLink="false">https://krasenslavov.com/?p=1227#comment-9390</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://krasenslavov.com/verifying-existing-attachments-in-the-database/#comment-9389&quot;&gt;Vladan&lt;/a&gt;.

&lt;strong&gt;Context:&lt;/strong&gt; When you use &lt;code&gt;wp_insert_attachment&lt;/code&gt; with a relative file path, WordPress internally converts this relative path into an absolute path and stores it in the &lt;code&gt;_wp_attached_file&lt;/code&gt; post meta field. By storing the absolute path in the &lt;code&gt;_wp_attached_file&lt;/code&gt; meta field, WordPress ensures that it can accurately locate and access the attachment file when needed. 

So, the main difference is in the SQL query:

&lt;code&gt;attachment_url_to_postid()&lt;/code&gt;:

&lt;code&gt;$sql = $wpdb-&#062;prepare( &quot;SELECT post_id, meta_value FROM $wpdb-&#062;postmeta WHERE meta_key = &#039;_wp_attached_file&#039; AND meta_value = %s&quot;, $path );&lt;/code&gt;

custom function:

&lt;code&gt;$sql = $wpdb-&#062;prepare( &quot;SELECT ID FROM {$wpdb-&#062;posts} WHERE guid = %s&quot;, $url );&lt;/code&gt;

Then the &lt;code&gt;_wp_attached_file&lt;/code&gt; has the full path and relative path for this we need to check the posts table where we have the guid.

So, to conclude the &lt;code&gt;attachment_url_to_postid()&lt;/code&gt; has better efficiency &#038; ease of use, but it doesn&#039;t cover 100% of the cases so it&#039;s reliability is less that the custom function.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://krasenslavov.com/verifying-existing-attachments-in-the-database/#comment-9389">Vladan</a>.</p>
<p><strong>Context:</strong> When you use <code>wp_insert_attachment</code> with a relative file path, WordPress internally converts this relative path into an absolute path and stores it in the <code>_wp_attached_file</code> post meta field. By storing the absolute path in the <code>_wp_attached_file</code> meta field, WordPress ensures that it can accurately locate and access the attachment file when needed. </p>
<p>So, the main difference is in the SQL query:</p>
<p><code>attachment_url_to_postid()</code>:</p>
<p><code>$sql = $wpdb-&gt;prepare( "SELECT post_id, meta_value FROM $wpdb-&gt;postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path );</code></p>
<p>custom function:</p>
<p><code>$sql = $wpdb-&gt;prepare( "SELECT ID FROM {$wpdb-&gt;posts} WHERE guid = %s", $url );</code></p>
<p>Then the <code>_wp_attached_file</code> has the full path and relative path for this we need to check the posts table where we have the guid.</p>
<p>So, to conclude the <code>attachment_url_to_postid()</code> has better efficiency &amp; ease of use, but it doesn&#8217;t cover 100% of the cases so it&#8217;s reliability is less that the custom function.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Vladan		</title>
		<link>https://krasenslavov.com/verifying-existing-attachments-in-the-database/#comment-9389</link>

		<dc:creator><![CDATA[Vladan]]></dc:creator>
		<pubDate>Wed, 14 Feb 2024 02:27:47 +0000</pubDate>
		<guid isPermaLink="false">https://krasenslavov.com/?p=1227#comment-9389</guid>

					<description><![CDATA[What&#039;s the difference between this approach and the built-in function `attachment_url_to_postid()`?

I also think that `guid` does not have to be a URL at all. I&#039;m not sure about this.]]></description>
			<content:encoded><![CDATA[<p>What&#8217;s the difference between this approach and the built-in function `attachment_url_to_postid()`?</p>
<p>I also think that `guid` does not have to be a URL at all. I&#8217;m not sure about this.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
