I'm trying to run this delete query in MYSQL (via phpMyAdmin) ANd I keep getting this error :
DELETE
p,pm
from wp_posts p
inner join wp_postmeta pm on pm.post_id = p.id
where p.id in (SELECT MIN( id ) AS min_id
FROM wp_posts inner join wp_postmeta on (wp_posts.ID=wp_postmeta.post_id and meta_key = 'old_id')
WHERE post_type = 'post'
GROUP BY meta_value
HAVING COUNT( * ) > 1)
any idea why ?
Looking to your code
You don't should use as in subselect for IN clause
DELETE p, pm
from wp_posts as p
inner join wp_postmeta as pm on pm.post_id = p.id
where p.id in (SELECT MIN( id )
FROM wp_posts
inner join wp_postmeta on (wp_posts.ID=wp_postmeta.post_id
and meta_key = 'old_id')
WHERE post_type = 'post'
GROUP BY meta_value
HAVING COUNT( * ) > 1)
Related
I'm using this query to get data from mysql. Can it be improved? It takes around 0.2s for each query...
I found some other way such as left join but I can't get all data with one query.
Or do I need to use two queries?
Select p.id, p.post_date, p.post_content, p.post_title, p.post_name, p.guid
for loop -> likes_count, dislikes_count, post_views_count, rate where post_id = p.id?
SELECT p.id, p.post_date, p.post_content, p.post_title, p.post_name, p.guid,
(SELECT guid FROM wp_posts WHERE id = (SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key = "_thumbnail_id")) AS thumbnail_url,
(SELECT GROUP_CONCAT(DISTINCT term_taxonomy_id) FROM wp_term_relationships WHERE object_id = p.id) AS term_taxonomy_id,
(SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key = "likes_count" LIMIT 1) AS likes_count,
(SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key = "dislikes_count" LIMIT 1) AS dislikes_count,
(SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key = "post_views_count" LIMIT 1) AS post_views_count,
(SELECT meta_value FROM wp_postmeta WHERE post_id = p.id AND meta_key = "rate" LIMIT 1) AS rate
FROM wp_posts AS p INNER JOIN wp_term_relationships AS t ON p.id = t.object_id
WHERE p.post_status="publish"
AND p.ping_status="open"
AND t.term_taxonomy_id = ? GROUP BY p.id LIMIT ?,?
[EDIT-1]
[Table structure]1
[EDIT-2]
The Output
{"id":11111,"post_date":"2016-01-02T05:46:58.000Z","post_content":"contenttttttt","post_title":"titleeeee","post_name":"postname","guid":"1255322","thumbnail_url":"thumb1957.jpg","term_taxonomy_id":"7","likes_count":"1","dislikes_count":"1","post_views_count":"538","rate":"50"}
Can any one re-write this SQL query to perform better?
At present this query takes about 29 seconds to execute in BlueHost server while it takes 6 seconds in HostGator.com server.
My intention is to delete all records exists in wp_postmeta table of post type='attachment' and with meta_key='_mycbgenie_managed_by'
DELETE FROM wp_postmeta
WHERE post_id IN
( SELECT ID FROM wp_posts
WHERE post_type = 'attachment'
AND post_parent IN
( SELECT ID FROM
( SELECT ID FROM wp_posts a
LEFT JOIN wp_postmeta b ON (a.ID = b.post_id)
LEFT JOIN wp_postmeta AS mt1 ON (a.ID = mt1.post_id)
WHERE post_type = 'product' AND mt1.meta_key = '_mycbgenie_managed_by'
) AS taskstodelete
)
)
DELETE PM FROM wp_postmeta as PM
INNER JOIN
wp_posts as P
ON PM.post_id=P.ID AND P.post_type='attachment'
INNER JOIN wp_posts as P2
ON P.post_parent=P2.ID
INNER JOIN wp_postmeta AS PM2
ON P2.ID = PM2.post_id
WHERE P2.post_type = 'product' AND PM2.meta_key = '_mycbgenie_managed_by'
I did own custom table in wordpress base, and now i need update the table using data from wopdpress's tables (thundbail link)
Here select query
SELECT p.post_parent, guid
FROM wp_postmeta AS pm
INNER JOIN wp_posts AS p ON pm.meta_value=p.ID
WHERE pm.post_id = 1552
AND pm.meta_key = '_thumbnail_id'
ORDER BY p.post_date DESC
LIMIT 1
Answer:
post_parent (fullproducts = postid) | guid (fullproducts = image)
1552 | URL
I tryed this code for update this table
UPDATE full_products, wp_postmeta AS pm
SET full_products.image = wp_postmeta.guid
INNER JOIN wp_posts AS p ON pm.meta_value=p.ID
WHERE pm.post_id = full_products.postid
AND pm.post_id = 1552
AND pm.meta_key = '_thumbnail_id'
ORDER BY p.post_date DESC
LIMIT 1
But this query doesnt work. Could you check it?
The way you used UPDATE with INNER JOIN have syntax error, the correct syntax is
UPDATE full_products a
LEFT JOIN wp_postmeta AS pm
ON a.postid = pm.post_id
LEFT JOIN wp_posts AS p
ON pm.meta_value=p.ID
SET a.image = pm.guid
WHERE pm.post_id = 1552
AND pm.meta_key = '_thumbnail_id'
After an importing error, there is many duplicate content from my posts.
Then, I try to delete this posts with that query :
DELETE
FROM wp_posts USING wp_posts
LEFT JOIN wp_postmeta pm ON wp_posts.ID = pm.post_id
AND pm.meta_key="_wpbdp[fields][6]"
LEFT JOIN wp_wpbdp_listing_fees wlf ON wp_posts.ID = wlf.listing_id
WHERE wp_posts.post_type="wpbdp_listing"
AND wp_posts.post_status="publish"
AND EXISTS (
SELECT NULL
FROM wp_posts p2
LEFT JOIN wp_postmeta pm2 ON p2.ID = pm2.post_id
AND pm2.meta_key="_wpbdp[fields][6]"
LEFT JOIN wp_wpbdp_listing_fees wlf2 ON p2.ID = wlf2.listing_id
WHERE p2.post_type="wpbdp_listing"
AND pm2.meta_value=pm.meta_value
AND p2.post_status="publish"
AND wlf2.category_id=wlf.category_id
)
Unfortunately, I can't do a SELECT statement who calls the same table that I want to delete.
Is there another solution ?
I tried something new, and it passed.
Following the #Nick's idea, I did that query :
DELETE p
FROM wp_posts p, wp_posts p2
WHERE p.post_title = p2.post_title
AND p.post_type="wpbdp_listing"
AND p2.post_type="wpbdp_listing"
AND p2.post_status="publish"
AND p.post_status="publish"
AND EXISTS(
SELECT NULL
FROM wp_wpbdp_listing_fees wlf
WHERE p.ID = wlf.listing_id
AND EXISTS (
SELECT NULL
FROM wp_wpbdp_listing_fees wlf2
WHERE wlf2.category_id = wlf.category_id
AND p.ID = wlf2.listing_id
)
)
AND EXISTS(
SELECT NULL
FROM wp_postmeta pm
WHERE p.ID = pm.post_id
AND pm.meta_key="_wpbdp[fields][6]"
AND EXISTS (
SELECT NULL
FROM wp_postmeta pm2
WHERE pm2.meta_key="_wpbdp[fields][6]"
AND p.ID = pm2.post_id
AND pm2.meta_value=pm.meta_value
)
)
AND p.ID < p2.ID
It's very ugly and not optimised, but it works !
By the way, thanks for your answers !
I'm trying to show latest Post Excerpt, Post Title and Featured Image on an ASP page. To simplify the query I add the Permalink as a custom field for each Post. I have a query that gets all except Featured Image and I have another query that gets Featured image but I can't work out how to merge them into one query.
// Gets Post Excerpt and Post Title
SELECT
wp_posts.id,
wp_posts.post_title,
wp_postmeta.meta_value,
wp_postmeta.meta_key,
wp_posts.post_excerpt
FROM
wp_postmeta
INNER JOIN wp_posts p ON (wp_postmeta.post_id = wp_posts.ID)
WHERE post_id IN (
SELECT wp_posts.id
FROM wp_posts
WHERE post_type = 'post'
AND post_status = 'publish'
AND meta_key = 'my_permalink'
ORDER BY post_date, wp_posts.id
)
ORDER BY wp_posts.post_date DESC, wp_postmeta.post_id
LIMIT 2
// Gets Featured Images for a Post
SELECT p.*
FROM wp_postmeta AS pm
INNER JOIN wp_posts AS p ON pm.meta_value=p.ID
WHERE pm.post_id = $ID
AND pm.meta_key = '_thumbnail_id'
ORDER BY p.post_date DESC
Can anyone help me merge these queries? Thanks.
Sample data to be returned:
ID | post_title | post_excerpt | meta_value_my_permalink | featured_image_guid
** UPDATE *
I've managed to get the following which works fine except I can't get more that one row as I get an error when I try and use 'IN' in a subquery
e.g. pm2.post_id IN (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 2)
SELECT
p.post_title,
p.post_excerpt,
pm.meta_value AS permalink,
p2.guid as thumbnail,
p2.post_title as image_alt
FROM
wp_postmeta pm
INNER JOIN wp_posts p ON (pm.post_id = p.ID),
wp_postmeta pm2
INNER JOIN wp_posts p2 ON (pm2.meta_value = p2.ID)
WHERE
pm.post_id = (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1) AND
pm.meta_key = 'my_permalink' AND
pm2.post_id = (SELECT wp_posts.id FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1) AND
pm2.meta_key = '_thumbnail_id'
It is difficult to see what you are trying to get without some sample data but I think that you know the post ID and need the info from both the my_permakink and _thumbnail_id keys?
Since I'm on a tablet I will give you the bare bones only and let you limit the result set and filter it.
Select *
From wp_posts p
Inner join
Wp_postmeta pm1 on p.id = pm1.post_id and metakey = 'my_permalink'
Inner join
Wp_postmeta pm2 on p.id = pm1.post_id and metakey = '_thumbnail_id'
This will give you every post that has an image with the info for both metakeys. If you want the info for posts without an image change the second inner join to a left join.
A bunch of joins and a view as the subquery has given me what I need.
SELECT
p.post_title,
p.post_excerpt,
pm.meta_value AS permalink,
p2.guid AS thumbnail,
p2.post_title AS thumbnail_alt
FROM
wp_postmeta pm
INNER JOIN wp_posts p ON (pm.post_id = p.ID)
INNER JOIN wp_postmeta pm2 ON (pm2.post_id = p.ID)
INNER JOIN wp_posts p2 ON (pm2.meta_value = p2.ID)
WHERE
pm.meta_key = 'my_permalink' AND
pm2.meta_key = '_thumbnail_id' AND
p.ID IN (SELECT * FROM vwLatestPostIds)
Use this to retrieve the post title, post id, post featured thumbnail url, post category etc -
"SELECT a.ID id, a.post_title title, a.post_content content,taxonomy, name, a.post_date_gmt postdate,max(c.guid) img_url
FROM kb_posts a
JOIN kb_term_relationships tr
ON (a.id = tr.object_id)
JOIN kb_term_taxonomy tt
ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy='category')
JOIN kb_terms t
ON (t.term_id = tt.term_id AND t.term_id = $user_id)
LEFT JOIN
(select post_parent, max(post_date_gmt) as latest_image_date from kb_posts
where post_type='attachment' GROUP BY post_parent) b
on a.id=b.post_parent
LEFT JOIN kb_posts c
on c.post_parent=a.id
and c.post_type='attachment'
and b.latest_image_date = c.post_date_gmt where c.guid IS NOT NULL
GROUP BY a.ID ORDER BY a.ID DESC LIMIT 5"