hello i am having a proble where it seems to not find is title is duplicate it either deleets all of them or leave them all the code is called on publish_post hook

$currpostid = get_the_title( $post_id );
        if(!empty(get_page_by_title($currpostid, OBJECT, 'post'))){
            error_log("found is:".print_r(get_page_by_title($currpostid, OBJECT, 'post')));
            error_log("title is:".$currpostid);
            //wp_trash_post( $post_id );
            //wp_delete_post( $post_id, $force_delete = true  );

when checking logs it seems it seems error_log("found is:".print_r(get_page_by_title($currpostid, OBJECT, 'post'))); results in 1

after check post title i had found there are sometimes extra spaces there which maybe had an issue after fixing that i still coudn't it so i found a plugin that does this checing if post is unique after checking the source code i ca up with and works perferctly

*$post_type ='post';$title = get_the_title( $post_id );;
            global $wpdb;

    $sim_titles = "SELECT post_title,ID FROM $wpdb->posts WHERE post_type = '{$post_type}' AND post_title LIKE '%{$title}%' AND ID != {$post_id} ";

    $sim_results = $wpdb->get_results($sim_titles);

    if($sim_results) {
        //wp_delete_post( $post_id, $force_delete = true  );
        wp_trash_post( $post_id );
  • I suggest you to roll back your edit and add your solution as an answer instead - then accept it. :) – Sally CJ Feb 6 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.