-1

I'm trying to use the following code to use sql to delete posts from my wordpress site that have the same value for the 'source_link' meta key. Anytime I run this code though, it will not delete duplicates and instead I get a database error on my site. Does anyone see anything wrong this code or know why I'm getting database errors?

$wpdb->query("DELETE p, pm1
FROM 
    $wpdb->posts as p, 
    $wpdb->postmeta as pm1, 
    $wpdb->postmeta as pm2 
WHERE 
    p.ID = pm1.post_id
    AND pm1.post_id > pm2.post_id 
    AND pm1.meta_key = 'source_link' 
    AND pm1.meta_key = pm2.meta_key 
    AND pm1.meta_value = pm2.meta_value");
arian1123
  • 216
  • 3
  • 14

1 Answers1

0

What database error are you getting?

I think your problem might be with how you're referring to multiple tables in the statement. I just found this question on Stack Overflow that says it needs to use a JOIN statement.

Community
  • 1
  • 1
bbrumm
  • 1,342
  • 1
  • 8
  • 13
  • My site no longer loads and instead you get a big fat databse error. I will try doing something like that. Im not an sql expert though so would you know how to use a join statement here? – arian1123 Nov 13 '14 at 16:39
  • It might be difficult to use a JOIN here because while the P and PM1 tables are linked using the = sign, the pm1 and pm2 are linked using a > sign. Try this: `DELETE p, pm1 FROM $wpdb->posts as p JOIN $wpdb->postmeta as pm1 ON p.ID = pm1.post_id JOIN $wpdb->postmeta as pm2 ON pm1.post_id > pm2.post_id WHERE AND pm1.meta_key = 'source_link' AND pm1.meta_key = pm2.meta_key AND pm1.meta_value = pm2.meta_value` – bbrumm Nov 17 '14 at 19:45