I’m trying to return those posts with post_type
= "attachment" , where their meta_value
(url) doesn’t appear in any post_content
.
For now I’m trying to give the meta_value
manually, which in this case is a image name speed1
I’ve cheked and there are some posts with the image "speed1.jpg" in the post_content
.
When I select those WITH the word in the content, it works, the problem arises when I try to select everything else but those with the word in the content, using the query:
SELECT
i.ID,
i.post_content,
i.post_type
FROM
wp_posts i
WHERE
i.post_type = 'attachment'
AND NOT EXISTS (SELECT * FROM wp_posts p WHERE p.post_type <> 'attachment' AND p.post_content LIKE "%speed1%")
this always returns empty. Why?
3
Answers
Could you do something like this
Literal values must be surrounded by single quotes and not by apostrophes (double quote). Double quote are requested to surround object name in SQL (table name, column name…).
In your query the part :
Is looking after a column nammed %speed1%
It is not the same as comparing to a litteral static value…
I think you will better do :
Here is the simple CTE that I am using as a crude mock of wp_posts for the following queries –
which looks like this as a table –
Your current sub-query in
NOT EXISTS
–always returns the row with ID 4 from my mock. This means
NOT EXISTS
always returns false as there is a result.If you change the sub-query to use the meta_value from the outer query you have something which makes more sense –
This returns the row with ID 2 as ‘speed2’ is not in the post_content of either of the two wp_posts of type ‘post’. I hope you are able to make sense of this simple demo.