Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set POST permalink/slug in wordpress using wp_insert_post

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

like image 217
Andy Avatar asked May 05 '11 12:05

Andy


People also ask

How do I create a Wordpress post code?

$new = array( 'post_title' => 'Our new post', 'post_content' => 'This is the content of our new post. ', 'post_status' => 'publish' ); This is the simplest way to create a new post, by defining its title, content and status.


1 Answers

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 );
like image 66
windyjonas Avatar answered Sep 21 '22 05:09

windyjonas