Make a plugin that lists duplicate posts, but comparing a custom field value

0 Votes
    160 Views

I’m trying to make a plugin show for me, all duplicate posts, but not use as a basis for comparison, post_title as all plugins do, I want to be based on a custom fields, called mypostID, all posts with that custom field that have their values repeated, leaves listed for deletion, the values are in this format: mypostID: 1098380

<?php
function get_post_id_by_meta_key_and_value($value) {
global $wpdb;
$meta = $wpdb->get_results("SELECT * FROM <code>&quot;.$wpdb->postmeta.&quot;</code> WHERE meta_value = ('".$value."')");
if (is_array($meta) && !empty($meta) && isset($meta[0])) {
$meta = $meta[0];
}   
if (is_object($meta)) {
return $meta->post_id;
}
else {
return false;
}
}
if (($_POST['key']=='delete')&&($_POST['postid_array']!='')){
$del_list=explode(",",$_POST['postid_array']);
foreach ($del_list as $postid) {
$title=get_the_title( $postid );
$delete = wp_delete_post($postid, true); //True force deletes the post and doesn't send it to the Trash
        if($delete)
            echo "<div id="message" class="updated fade"><p>post $title deleted successfully!</p></div>n";
        else
            echo "<div id="message" class="updated fade"><p>post $title was not deleted!</p></div>n";
        }

}

echo 'This tool will check for duplicate posts in your wordpress site and remove them.<br>';
if ($_POST['key']=='check'){
set_time_limit(0);
$array=get_unique_post_meta_values('mypostID', 'post'); 
//$deleted_list = array();

foreach ($array as $value) { 
     // if (($key = array_search($value, $deleted_list, TRUE))!==false) {
   $value = array_unique( array_diff_assoc( $array, array_unique( $array ) ) ); 
         $results[] = $value;
      }
    }

echo "<br>" ;
if (count($results)<>0){
$i=0;
echo "There are ".count($results)." duplicate post(s) in your wordpress site.<br>";
foreach ($results as $result) { 
$i++;
$postid= get_post_id_by_meta_key_and_value($result);
$postid_array.=$postid.",";
echo "$i. <a href=".post_permalink( $postid )." target='_blank'>".get_the_title( $postid )."</a>";
echo "<br>" ;
}
$postid_array=rtrim($postid_array,",");
echo'<br><form name="delvid" method="post" action=""><input name="key" value="delete" type="hidden" /><input name="postid_array" value="'.$postid_array.'" type="hidden" /><input type="submit" value="Delete all all duplicate posts" id="Submit"  class="button-primary"/></form>';
} else {echo "Cool! All posts are uniques!";}
}else{
echo'<br><form name="delvid" method="post" action=""><input name="key" value="check" type="hidden" /><input type="submit" value="Check" id="Submit"  class="button-primary"/></form>';
}

?>

but the problem is that the line if (($ key = array_sea … seems to be mandatory, every time I delete it from the code, the check button disappears from my wp dashboard, can anyone help me with this?


Please signup or login to answer this question.