最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mysql - Find locations of all featured images of draft posts via SQL

programmeradmin2浏览0评论

I'm trying to get the file locations of all featured images used in draft type posts via SQL and phpmyadmin.

posts type attachment should hold the location of the image in the guid column.

SELECT voybp_posts.guid
FROM voybp_posts
WHERE voybp_posts.ID IN (
    SELECT voybp_postmeta.meta_value
    FROM voybp_postmeta
    WHERE voybp_postmeta.post_id IN (
        SELECT voybp_posts.ID
        FROM voybp_posts
        WHERE voybp_posts.post_status="draft"))

This script should be good but its very complex and takes forever to execute, any way for me to simpplify it?

I'm trying to get the file locations of all featured images used in draft type posts via SQL and phpmyadmin.

posts type attachment should hold the location of the image in the guid column.

SELECT voybp_posts.guid
FROM voybp_posts
WHERE voybp_posts.ID IN (
    SELECT voybp_postmeta.meta_value
    FROM voybp_postmeta
    WHERE voybp_postmeta.post_id IN (
        SELECT voybp_posts.ID
        FROM voybp_posts
        WHERE voybp_posts.post_status="draft"))

This script should be good but its very complex and takes forever to execute, any way for me to simpplify it?

Share Improve this question edited Mar 28, 2020 at 14:51 Adilicious asked Mar 28, 2020 at 13:49 AdiliciousAdilicious 1193 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Alright this worked for me:

First run the bellow script to generate the file locations of all files used for draft posts.

SELECT voybp_posts.guid
FROM voybp_posts
WHERE voybp_posts.ID IN (SELECT voybp_postmeta.meta_value
                         FROM voybp_postmeta
                         WHERE voybp_postmeta.post_id IN (
                             SELECT voybp_posts.ID
                             FROM voybp_posts
                             WHERE voybp_posts.post_status="draft")
                         AND voybp_postmeta.meta_key="_thumbnail_id")

Export this as a CSV file and save it to desktop, then using notepad++ or a similar program use find/replace to add rm to the beginning of every line.

Next SSH into your server and execute the rm commands.

Next you modify the above script and run it to delete the attachment files in the database that lead to nothing.

DELETE
FROM voybp_posts
WHERE voybp_posts.ID IN (SELECT voybp_postmeta.meta_value
                         FROM voybp_postmeta
                         WHERE voybp_postmeta.post_id IN (
                             SELECT voybp_posts.ID
                             FROM voybp_posts
                             WHERE voybp_posts.post_status="draft")
                         AND voybp_postmeta.meta_key="_thumbnail_id")

Finally we find and delete the draft posts.

DELETE
FROM voybp_posts
WHERE voybp_posts.post_status="draft"
AND voybp_posts.post_type="post"

Hope this helps

发布评论

评论列表(0)

  1. 暂无评论