wp_publish_post breaks permalinks

Although the name of the function wp_publish_post() suggests that it can be used to publish a post it should obviously not be used to publish a draft post programmatically.

The way to do so is to use wp_update_post() and set the post status manually to publish:

<?php
// Update post with the ID 42
$postData = [ 'ID' => 42, 'post_status' => 'publish' ];
wp_update_post( $postData );

This will create a valid value for the post_name field and thus a proper permalink.

The function wp_publish_post() is used by WordPress only for publishing future posts during a scheduled hook (see check_and_publish_future_post()). The reason why this won’t break permalinks is, that posts with status future already have a valid value for post_name created by wp_insert_post().

Leave a Comment