16

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

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Andy
  • 161
  • 1
  • 1
  • 3

1 Answers1

36

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 );
windyjonas
  • 2,272
  • 17
  • 19
  • 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 '12 at 11:27
  • This is what I tried but getting an error `'post_name' => '$title'.'-'$pid',` – Anagio Dec 22 '12 at 11:29
  • 8
    @anagio Try adding this: `, 'post_name' => sanitize_title_with_dashes($title,'','save')` – pixeline Jan 29 '13 at 23:12