teone
teone

Reputation: 2183

Wordpress - Get all posts without image?

is there any way to find all the post without any associated images (neither inline or attachment) from Sql?

Or anyone can explain me how does wordpress store the images cause it is not so clear to me. I found out that they could be inline in text or stored in wp_postmeta with meta_key = "_wp_attached_file" or in wp_posts with post_type = "attachment".

Is this right?

Any help would be appreciated.

Thanks in advance.

Upvotes: 0

Views: 2176

Answers (2)

teone
teone

Reputation: 2183

Thanks, I've added:

AND ID not in (select post_id as p from wp_postmeta where meta_key like "_wp_attached_file")

but it still return too many results.

I've seen that in post_content of my results I've some images declared like:

<img src="/public/Username/filename.jpg">

And that query doen't exlude that post. I got a good result using:

SELECT DISTINCT(p.ID), p.post_title, p.post_content FROM `wp_posts` p
LEFT JOIN wp_posts im ON p.ID = im.post_parent AND im.post_type = "attachment" 
WHERE p.post_status ='publish' 
    AND p.post_type = "post" 
    AND im.ID IS NULL
    AND p.post_content NOT REGEXP 'src=".*"' 

Upvotes: 1

Fabio
Fabio

Reputation: 791

Wordpress manages media (images,text documents, etc) by creating an attachment post for holding the information about that media and it's relation (if any) with other post/posts.

To retrieve all posts without any image attached you can execute a query like this:

select * from wp_posts where id not in (select post_id as p from wp_postmeta where meta_key like "_thumbnail_id")

Upvotes: 1

Related Questions