14

I m writing simple script using wp_insert_post() to post article in blog. but one problem here, I want to make Title and slug of an URL different.

How to achieve this?

for example:

Title: How to make your diet success

Slug: 7-ways-to-make-succes-Diet

1 Answer 1

34

post_title sets the title, and post_name sets the slug. So:

// Create post object
$my_post = array(
     'post_title' => 'How to make your diet success',
     'post_name' => '7-ways-to-make-succes-Diet',
     'post_content' => 'my content',
     'post_status' => 'publish',
     'post_author' => 1,
     'post_category' => array(8,39)
  );

// Insert the post into the database
wp_insert_post( $my_post );
3
  • Hi, is it possible to append the post ID to the end of post_name, so that the slug generated would look like `hello-world-123' where 123 is the post ID being inserted?
    – Anagio
    Dec 22, 2012 at 11:27
  • This is what I tried but getting an error 'post_name' => '$title'.'-'$pid',
    – Anagio
    Dec 22, 2012 at 11:29
  • 8
    @anagio Try adding this: , 'post_name' => sanitize_title_with_dashes($title,'','save')
    – pixeline
    Jan 29, 2013 at 23:12

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.