When I insert a post I’ve found that there is a slow call to wp_unique_post_slug from within insert_post (In the database I’m using to test there is 155,000 posts – live would have considerably more). As I don’t believe a slug is important for what I’m doing (a person who is browsing our site will probably never see a single page view for this post type), is there anything I can do that can either speed up the call or (better yet) eliminate it.
Looking at the code for insert_post, I couldn’t see a way of doing it without modifying core (which is something I’d rather not do). The code I’m using is:
$post = array(
'post_title' => 'post_title',
'post_type' => 'character',
'post_status' => 'publish'
);
$post_id = wp_insert_post( $post );
Edit:
As it appears people are missing the information that I have written in this question. Here is the issue as clear as I can make it.
The Problem:
Slow performance. I have tracked this down to the function wp_unique_post_slug
called from within wp_insert_post
The code that calls wp_insert_post
:
$post = array(
'post_title' => 'post_title',
'post_type' => 'character',
'post_status' => 'publish'
);
$post_id = wp_insert_post( $post );
(yes they are all strings and not variables being assigned to $post)
Other information:
The post status needs to be ‘publish’ as these posts are presented to the user (although not in the standard WordPress manner).
I could fix it by editing core, but this is a BAD idea for a number of reasons.
A slug is not important as the user shouldn’t be going to a single page view of this content type.
2 Answers
I know this is really old question, but today I was dealing with something similar, and I have found a solution – if wp_unique_post_slug()
calling is performance bottleneck, and post_name
value is not important, then set post_name manualy to some random value.
My wp_insert_post
post action was taking too long lately (20-30s). It saves custom post type order
not visible to visitors, with post_title = "Order"
(post title does not matter). That resulted in thousands of posts with title “Order”, and WP was automatically generating post names (post_name
) as “order-1”, “order-2”, “order-3”, … “order-6152” …
And with every wp_insert_post
WP searched for free post name starting from “order” and incrementing suffixed number – and every step was one DB query. So in my case WP did more than 6,000 DB queries before inserting one post to database.
Setting random post_name in wp_insert_post() call disables wp_unique_post_slug() call.