1

i have two post duplicate Post Titles: "Love Story".

i want code Automatically add a random words if Duplicate Post Titles, like post 2 duplicate Titles ==> auto change post 2 titles like: "Love Story Two".

and if user up post 3, duplicate Titles again => auto add random words in post titles like : "Love story three".

thanks a lot

3
  • 1
    When should these words be added? When the post is published? Commented Feb 6, 2019 at 19:18
  • WordPress doesn't care too much about duplication of post titles, it's the slugs that matter.
    – rudtek
    Commented Feb 6, 2019 at 21:52
  • @KrzysiekDróżdż yes, When the post is published.
    – Duy Hữu
    Commented Feb 6, 2019 at 22:55

1 Answer 1

1

Please give this a shot. I didn't test it, so let me know if it has any errors. Basically, it will check the title before saving and run a loop checking to see if it can get a post by that title. If it can, it will add a new suffix and keep trying.

As you can see, you'll have to add more number to the $suffixes array to allow it go count farther. Alternatively, you could use The NumberFormatter class if available.

add_filter( 'wp_insert_post_data' , 'filter_post_title' , '99', 2 );
function filter_post_title( $data , $postarr ) {
    if( !$postarr['ID'] ) { // Publishing for the first time
        $suffixes = array( 'Two', 'Three', 'Four' );
        $count = 0;
        $original_title = $data['post_title'];
        $new_title = $original_title;
        while( get_page_by_title( $new_title, 'object', 'post' ) ) {
            $new_title = $original_title . " " . $suffixes[$count];
            $count++;
        }
        $data['post_title'] = $new_title;
    }
    return $data;
}

You could write it with fewer variables, but I feel this way is more readable.

Edit: The downside to this is that it has to query the database each time it increments. So, to get to "Five" it would require five queries. If you start having it count to high numbers you will start to notice the saving process taking longer. In that case, querying all the posts that start with the current title and then parsing them to find the current number would be more efficient.

5
  • Thanks for the help, but it doesn't work. I added the code but published the post with duplicate titles, still not adding random words.
    – Duy Hữu
    Commented Feb 7, 2019 at 8:41
  • Sorry, I forgot that get_page_by_title() needs to be set to look at posts. I tested it and it works now. Please test it with all save cases to make sure you don't get any unexpected results. It may not work perfectly, but it is a good start. Commented Feb 8, 2019 at 1:48
  • thank you so much, it work!!! <3<3<3
    – Duy Hữu
    Commented Feb 8, 2019 at 20:11
  • But there was a problem, when I edited and updated the post, "Post Titles + Two" changed "Post Title + Two + Two", meaning it would be changed every time I updated !! Is there any way Just change the title when publish, Updates are not altered title?
    – Duy Hữu
    Commented Feb 8, 2019 at 20:40
  • An if statement shoudl take care of that! I added it to the code, but didn't test it. Please let me know if there are issues. When this filter is executed, it is before the database query, so if the post is being saved to the database for the first time it will not have an ID yet. This if statement only runs our title replacement if this ID is not present. Commented Feb 9, 2019 at 2:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.