i am using below insert query adding custom filed for all wordpress post, but getting Sql Syntax error i dont know where i m doing wrong
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'CustomField'
AS meta_key 'MyValue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'CustomField')
'' AND post_type = 'post';
you missed comma for separation of columns and single quote to wrap string.
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id,
'CustomField' AS meta_key,
'MyValue' AS meta_value
FROM wp_posts
WHERE ID NOT IN
(SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'CustomField')
AND post_type = 'post';
An alternative of using NOT IN is by joining two tables,
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT a.ID AS post_id,
'CustomField' AS meta_key,
'MyValue' AS meta_value
FROM wp_posts a
LEFT JOIN wp_postmeta b
ON a.ID = b.post_id AND
b.meta_key = 'CustomField'
WHERE a.post_type = 'post' AND
b.post_id IS NULL
Related
I need get value if two rows have same value. I need get post_id one field 145 in output.
Should i use JOIN or not? Or maybe some simpler/faster way to do this?
But this is not correct one...But i need someting like this, when barcode and _sku values are SAME.
SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'barcode' = '_sku' AND meta_value = '123';
Getting error and checking just barcode field...
Warning: #1292 Truncated incorrect DECIMAL value: '_sku'
Since MySQL 8.0 you can use INTERSECT:
SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'barcode' AND meta_value = '123'
INTERSECT
SELECT post_id
FROM wp_postmeta
WHERE meta_key = '_sku' AND meta_value = '123';
https://sqlize.online/sql/mysql80/d0a7123a10d11065d9f0609044c70d35/
Another way is use GROUP BY and HAVING:
SELECT post_id
FROM wp_postmeta
WHERE meta_key IN ('barcode', '_sku') AND meta_value = '123'
GROUP BY post_id
HAVING COUNT(DISTINCT meta_key) = 2;
https://sqlize.online/sql/mysql80/594f4b262c55c10d4958f3bc54c7353b/
SELECT post_id
FROM wp_postmeta
WHERE
meta_key IN ('barcode')
AND meta_value = '123'
AND EXISTS (SELECT post_id FROM wp_postmeta pm WHERE meta_key IN ('_sku')
AND meta_value = '123' AND pm.post_id = wp_postmeta.post_id)
;
I want add to wp_postmeta meta_key record if this doesn't exists.
I had this query, but from something strange reason it doesn't work anymore. Please help!
INSERT INTO wp_postmeta (meta_key, meta_value, post_id)
SELECT '_update_date', '', ID FROM wp_posts WHERE wp_posts.post_type='product'
AND NOT EXISTS (SELECT meta_key FROM wp_postmeta WHERE meta_key = '_update_date' );
If you wanted to insert into wp_postmeta IF
SELECT '_update_date', '', ID FROM wp_posts WHERE wp_posts.post_type='product'
Doesn't EXISTS.....
I think this query will work . You were confused with syntax and missed where
`INSERT INTO wp_postmeta (meta_key, meta_value, post_id)
SELECT meta_key FROM wp_postmeta WHERE meta_key = '_update_date'
WHERE NOT EXISTS(SELECT '_update_date', '', ID FROM wp_posts WHERE
wp_posts.post_type='product')`
Hope this helps.
I'm trying to make a mysql query which select all the posts from wp_posts and select the post_title and post_content from it and then select the table wp_postmeta and show the meta_value where meta_key is equal with "_wp_attached_file".
I've tried doing this:
SELECT post_title, post_content, meta_value FROM wp_posts, wp_postmeta WHERE ID = post_id and meta_key = '_wp_attached_file'
But this will only show if there is a meta_key equal to '_wp_attached_file'.
The query should show all posts and if it has a meta_key equal to '_wp_attached_file'. Then show meta_value else just show nothing or NULL?
How can i do this?
Use LEFT JOIN
SELECT post_title, post_content, meta_value
FROM wp_posts
LEFT JOIN
wp_postmeta ON ID = post_id
and meta_key = '_wp_attached_file'
I need to show the contents of two tables; my query is this:
SELECT ID,
post_author,
post_content,
post_title,
post_name,
guid,
post_type,
meta_id,
post_id,
meta_key,
meta_value
FROM wp_posts,
wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND post_author IN (2);
The meta_key column stores the TYPE of content I need like website, latitude, longitude, phone, email. Is there a way to make this column explode to another columns? For example:
ID,
post_author,
...,
meta_id,
post_id,
meta_key,
meta_value,
website(its a meta_key result),
latitude(another meta_key result),
longitude(anoter meta_key result)...
It is not clear what are you trying to do, but I think you need to display the meta key and values as columns' headers with the meta values as the values for each column. In this case you can do something like this:
SELECT
ID,
post_author,
post_content,
post_title,
post_name,
guid,
post_type,
meta_id,
post_id,
MAX(CASE WHEN meta_key = 'website' THEN meta_value END) AS website,
MAX(CASE WHEN meta_key = 'latitude' THEN meta_value END) AS latitude,
MAX(CASE WHEN meta_key = 'longitude' THEN meta_value END) AS longitude,
...
FROM wp_posts
INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
WHERE post_author IN (2)
GROUP BY Id, post_author, ...
Things to note:
Try to avoid the old JOIN syntax that you used in your query; it is a bad habit , and use the ANSI-SQL-92 syntax using the JOIN key word instead like what I did.
I used the MAX as a work around to pivot the rows into columns, you will need to list the columns that are not in the aggregate function in the GROUP BY clause.
I have a table wp_postmeta with columns called meta_key and meta_value. I have 5 records in meta_key (location,area,price,bedrooms,bathrooms)
For example, I want to find a hotel in Texas with 2 bathrooms:
select post_id from wp_postmeta where meta_key = 'location' and meta_value = 'texas' and where meta_key = 'bathrooms' and meta_value= '2';
I know the above SQL command is not valid. Can anyone please help me to achieve the above result?
You can try mysql subquery:
select post_id
from wp_postmeta
where meta_key = 'location' and meta_value = 'texas'
and post_id IN (select post_id
from wp_postmeta
where meta_key = 'bathrooms' and meta_value= '2')