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");
 
     
    