I have a post publish program run like this:

if (!post_exists($title)) :

$my_post = array(
    'post_content'=>'my contents',


That is ,check if the title already exists before wp_insert_post actually publish it.

But the problem is I got 50,000 + posts on my site ,so it's very slow.

Do you know way to improve it?

I also tried:

get_page_by_title( $page_title, $output, $post_type );

It's slower than the post_exists:)

  • There's no index on the post_title column, but post_name is indexed, maybe that could help? But we should note that there can be posts with same title but different slugs. – birgire Feb 13 '17 at 11:29
  • Can I simply make post_title unique? Is there any harms? thanks. – 吉 宁 Feb 13 '17 at 11:59
  • Not sure if that's a good idea, you might be able to make it work with your plugin, but I guess you would get a db error when you try to post a title duplicate from the backend.You might of course play with a fixed length post title index on your test install, but then you would need to take care of too long titles, so I'm not sure how good idea that is in your case. Another approach might be to create e.g. a new custom table with an index, that you keep in sync or a temporary view. – birgire Feb 13 '17 at 13:54
  • Thank you .Fixed Lenth is not the problem.By seting the post_title to unique and varchar(100),it should work almost all of the time.But I'm not sure what potential risk this might have. I better leave the default db structure alone and try the postname you suggested. – 吉 宁 Feb 13 '17 at 14:38

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.