1

Is there any way to add a custom URL to the wp_insert_post function?

$my_post = array(
    'post_title'    => wp_strip_all_tags( $_POST['post_title'] ),
    'post_content'  => $_POST['post_content'],
    'post_status'   => 'publish',
    'post_author'   => 1,
    'post_category' => array( 8,39 )
 );
wp_insert_post( $my_post );
3
  • What do you mean by custom URL ?
    – Sumit
    Commented Apr 15, 2016 at 10:30
  • If you mean post slug, then use post_name parameter.
    – Max Yudin
    Commented Apr 15, 2016 at 11:12
  • If i put "Lorem ipsum" as name, the url will be "/lorem-ipsum". But i need to put to custom url for the post. In this case, i want the url to be "/myurl" instead of "/lorem-ipsum".
    – Senneville
    Commented Apr 15, 2016 at 11:26

2 Answers 2

1

You can use post_name parameter.

$my_post = array(
    'post_title'    => wp_strip_all_tags( $_POST['post_title'] ),
    'post_content'  => $_POST['post_content'],
    'post_status'   => 'publish',
    'post_author'   => 1,
    'post_category' => array( 8,39 ),
    'post_name'     => 'your-url'
 );
wp_insert_post( $my_post );
2
  • How can i add a condition to test if the "post_name" exist then update the post ?
    – Senneville
    Commented Apr 19, 2016 at 16:34
  • @Senneville if ( get_page_by_title( $_POST['post_title'] ) === null ) {... doesn't exists { else {...
    – rob.m
    Commented Aug 9, 2019 at 4:24
-1

Seems like you might be looking for this button:

enter image description here

1
  • 1
    No, the post should be created automaticaly...
    – Senneville
    Commented Apr 15, 2016 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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