• 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 posts(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 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?

    • This topic was modified 7 years, 4 months ago by loganivan.
    • This topic was modified 7 years, 4 months ago by loganivan.
    • This topic was modified 7 years, 4 months ago by loganivan.
    • This topic was modified 7 years, 4 months ago by loganivan.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter loganivan

    (@loganivan)

    I have updated the code:

    
    <?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);
    $array1=get_unique_post_meta_values('mypostID', 'post'); 
    //$value = array();
     
         // $dupl = array_unique( array_diff_assoc( $array, array_unique( $array ) ) ); 
          //if (($key = array_search($array, $dupl, TRUE))) { 
          if ($dupl = array_unique(array_diff_assoc($array1, array_unique($array1)))){
             $results[] = $dupl;
          }
      // }
    
    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 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>';
    }
    
    ?>
    

    was better read now.

    • This reply was modified 7 years, 4 months ago by loganivan.
    • This reply was modified 7 years, 4 months ago by loganivan.
    • This reply was modified 7 years, 4 months ago by loganivan.
    • This reply was modified 7 years, 4 months ago by loganivan.
    Thread Starter loganivan

    (@loganivan)

    Updated againd:

    
    <?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'); 
    function get_duplicates( $array ) {
        return array_unique( array_diff_assoc( $array, array_unique( $array ) ) );
    }     
          $value = get_duplicates( $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 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>';
    }
    
    ?>
    

    Everything seems right, but I do not know why the duplicate posts list is not presented to me? I only get a string as output!:

    This tool will check for duplicate posts in your wordpress site and remove them.
    There are 1 Duplicate post(s) in your wordpress site.
    1.
    //(only: 1.)

    • This reply was modified 7 years, 4 months ago by loganivan.
    • This reply was modified 7 years, 4 months ago by loganivan.
    • This reply was modified 7 years, 4 months ago by loganivan.
    Moderator bcworkz

    (@bcworkz)

    If get_unique_post_meta_values() is true to its name, there would be no duplicates in the returned array, so when you do get_duplicates() on that array, naturally there would be no duplicates, so no list could ever be generated.

    What you want to do is query postmeta for all meta values matching the meta_key (duplicate or not), along with the associated post IDs. From that list you can identify duplicate meta values and delete all but the first post with the same value.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Make a plugin that lists duplicate posts, but comparing a custom field value!’ is closed to new replies.