0

When calling wp_insert_post() see api there seems to be no possibility to set the post slug / the post permalink of the new entry.

e.g. is there a way to check if:

a string is possible to use as post-slug, if not available return error.

  • What about 'post_name' => [ <string> ] // The name (slug) for your post ? – denis.stoyanov Jan 4 '16 at 19:52
1

post_name is the slug.

'post_name' => [ <string> ] // The name (slug) for your post

You'll need to query for a post with matching slug before inserting a new post, WordPress will just append a number to make it unique if there is a duplicate.

  • so probably with this code to check the slug first <?php $the_slug = 'my_slug'; $args = array( 'name' => $the_slug, 'post_type' => 'post', 'post_status' => 'publish', 'numberposts' => 1 ); $my_posts = get_posts($args); if( $my_posts ) : echo 'ID on the first post found ' . $my_posts[0]->ID; endif; ?> – Toskan Jan 4 '16 at 19:58
  • using args this way is better: $args = array('name' => $the_slug, 'post_type' => 'any', 'post_status' => 'any', 'numberposts' => 1); – Toskan Jan 7 '16 at 19:27

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.