2

How can i create unique slug while inserting a new post..

I know i can query posts and compare the records to create unique slug but

I want to insert a post with unique slug at the same time.

Lets say i have post title and needed data for the post and want to insert it with wpquery Does wordpress have a function to handle all by itself..

When i insert a post with st. like

$my_post = array(
  'post_title'    => 'My post',
  'post_content'  => 'This is my post.',
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array(8,39)
);

// Insert the post into the database
wp_insert_post( $my_post );

Doest it handle the slug automatically..

I want to insert this post and open it with php redirect after insert..

  • 1
    If you do not specify a post_name parameter, WordPress will create the slug for you automatically based on the post_title. If a post already exists with the same post_name, WordPress will append a number to the end of the slug, such as {slug}-2 and {slug}-3 etc. – Adam Jun 9 '15 at 7:54
  • Not sure if I understand what you want to do. Do you want to insert a post a redirect to the post after it has been inserted? – cybmeta Jun 9 '15 at 7:55
  • Exactly.. When i searched i found out that wp_insert_post should create slug automatically. I will try that.. I appreciate any help about this also.. – kutlus Jun 9 '15 at 7:58
2

You don't have to think about it - WordPress will take care of this.

Let's take a look at wp_insert_post source code...

On line 3203 you'll find:

if ( empty($post_name) ) {
    if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
        $post_name = sanitize_title($post_title);
    } else {
        $post_name = '';
    }
} else {
    // On updates, we need to check to see if it's using the old, fixed sanitization context.
    $check_name = sanitize_title( $post_name, '', 'old-save' );
    if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) {
        $post_name = $check_name;
    } else { // new post, or slug has changed.
        $post_name = sanitize_title($post_name);
    }
}

So if no post_name is set, WP will generate it from post_title.

And then on line 3325:

   $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );

So WP will take care of uniqueness of post_name.

|improve this answer|||||
1

WordPress will take care about unique slug. If you want to redirect to the post after it has been created, you can get the permalink using the post ID which is returned by wp_insert_poost() if it was successfull:

$my_post = array(
  'post_title'    => 'My post',
  'post_content'  => 'This is my post.',
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array(8,39)
);

// Insert the post into the database
$post_id = wp_insert_post( $my_post );

// Check there was no errors
if( $post_id && ! is_wp_error( $post_id ) ) {

    wp_redirect( get_the_permalink( $post_id ) );
    exit;

}
|improve this answer|||||

Your Answer

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

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