wodpress在删除文章的同时如何把附件一起删掉

wordpress网站建设,wordpress建站在这里教您,

WordPress删除文章时,文章里面所带的那些图片附件系统是不会自动删除,占用了网站空间,因此下面说明通过几行代码的简单方式实现在删除文章时自动删除缩略图以及图片附件,这样就不用手动去媒体库寻找并删除,准确而且效率高。

wordpress-logo-stacked-rgb

<div id="crayon-597a01bfa65e2443520689-1" class="crayon-line"><span class="crayon-c">/* 删除文章时删除图片附件</span></div>
<div id="crayon-597a01bfa65e2443520689-2" class="crayon-line"><span class="crayon-c">/* ———————— */</span></div>
<div id="crayon-597a01bfa65e2443520689-3" class="crayon-line"><span class="crayon-t">function</span> <span class="crayon-e">delete_post_and_attachments</span><span class="crayon-sy">(</span><span class="crayon-sy">$</span><span class="crayon-v">post_ID</span><span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-597a01bfa65e2443520689-4" class="crayon-line"><span class="crayon-m">global</span> <span class="crayon-sy">$</span><span class="crayon-v">wpdb</span><span class="crayon-sy">;</span></div>
<div id="crayon-597a01bfa65e2443520689-5" class="crayon-line"><span class="crayon-c">//删除特色图片</span></div>
<div id="crayon-597a01bfa65e2443520689-6" class="crayon-line"><span class="crayon-sy">$</span><span class="crayon-v">thumbnails</span> <span class="crayon-o">=</span> <span class="crayon-sy">$</span><span class="crayon-v">wpdb</span><span class="crayon-o">-></span><span class="crayon-e">get_results</span><span class="crayon-sy">(</span> <span class="crayon-s">"SELECT * FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID"</span> <span class="crayon-sy">)</span><span class="crayon-sy">;</span><span class="crayon-h">  </span></div>
<div id="crayon-597a01bfa65e2443520689-7" class="crayon-line"><span class="crayon-st">foreach</span> <span class="crayon-sy">(</span> <span class="crayon-sy">$</span><span class="crayon-e">thumbnails </span><span class="crayon-st">as</span> <span class="crayon-sy">$</span><span class="crayon-i">thumbnail</span> <span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-597a01bfa65e2443520689-8" class="crayon-line"><span class="crayon-h">     </span><span class="crayon-e">wp_delete_attachment</span><span class="crayon-sy">(</span> <span class="crayon-sy">$</span><span class="crayon-v">thumbnail</span><span class="crayon-o">-></span><span class="crayon-v">meta_value</span><span class="crayon-sy">,</span> <span class="crayon-t">true</span> <span class="crayon-sy">)</span><span class="crayon-sy">;</span><span class="crayon-h">  </span></div>
<div id="crayon-597a01bfa65e2443520689-9" class="crayon-line"><span class="crayon-sy">}</span></div>
<div id="crayon-597a01bfa65e2443520689-10" class="crayon-line"><span class="crayon-c">//删除图片附件</span></div>
<div id="crayon-597a01bfa65e2443520689-11" class="crayon-line"><span class="crayon-sy">$</span><span class="crayon-v">attachments</span> <span class="crayon-o">=</span> <span class="crayon-sy">$</span><span class="crayon-v">wpdb</span><span class="crayon-o">-></span><span class="crayon-e">get_results</span><span class="crayon-sy">(</span> <span class="crayon-s">"SELECT * FROM $wpdb->posts WHERE post_parent = $post_ID AND post_type = 'attachment'"</span> <span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-597a01bfa65e2443520689-12" class="crayon-line"><span class="crayon-st">foreach</span> <span class="crayon-sy">(</span> <span class="crayon-sy">$</span><span class="crayon-e">attachments </span><span class="crayon-st">as</span> <span class="crayon-sy">$</span><span class="crayon-i">attachment</span> <span class="crayon-sy">)</span> <span class="crayon-sy">{</span></div>
<div id="crayon-597a01bfa65e2443520689-13" class="crayon-line"><span class="crayon-h">     </span><span class="crayon-e">wp_delete_attachment</span><span class="crayon-sy">(</span> <span class="crayon-sy">$</span><span class="crayon-v">attachment</span><span class="crayon-o">-></span><span class="crayon-v">ID</span><span class="crayon-sy">,</span> <span class="crayon-t">true</span> <span class="crayon-sy">)</span><span class="crayon-sy">;</span><span class="crayon-h">  </span></div>
<div id="crayon-597a01bfa65e2443520689-14" class="crayon-line"><span class="crayon-sy">}</span><span class="crayon-h">  </span></div>
<div id="crayon-597a01bfa65e2443520689-15" class="crayon-line"><span class="crayon-sy">$</span><span class="crayon-v">wpdb</span><span class="crayon-o">-></span><span class="crayon-e">query</span><span class="crayon-sy">(</span> <span class="crayon-s">"DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID"</span> <span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-597a01bfa65e2443520689-16" class="crayon-line"><span class="crayon-sy">}</span></div>
<div id="crayon-597a01bfa65e2443520689-17" class="crayon-line"><span class="crayon-e">add_action</span><span class="crayon-sy">(</span><span class="crayon-s">'before_delete_post'</span><span class="crayon-sy">,</span> <span class="crayon-s">'delete_post_and_attachments'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>

将上述代码放到主题functions.php文件中即可使用,其在删除文章时先执行函数内容,删除特色图片以及图片附件,如果在使用action delete_post而不是before_delete_post将导致删除文章后因媒体附件与文章关联已取消而无法正确删除。

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/259328.html

(0)
上一篇 2022年5月21日
下一篇 2022年5月21日

相关推荐

发表回复

登录后才能评论