Get the permalink of a future post with WordPress

If you need to retrieve the permalink of a future post, WordPress gives you this possibility. In this article, I’m going to guide through the various steps of this process.

Get the status of the post

This instruction can be used to get the status of the post:

$post_status = get_post_status($id);

This function returns the status of the post associated with the provided post ID. If the post ID is not provided, the status of the current post will be returned.

The following list includes the statuses used by WordPress:

  • publish
  • future
  • draft
  • pending
  • private
  • trash
  • auto-draft
  • inherit

In our case, we need to identify if the post is a future post. (a post scheduled to be published in a future date) For this reason the result of the get_post_status() function should be equal to “future”.

Generate the future permalink of the post

If the get_permalink() function is used to retrieve the permalink of a future post, the result will be an URL with included URL parameters, that ignores any rules defined in the Settings -> Permalinks menu.

This is an example of an URL generated by get_permalink() for a future post:

https://example.com/?p=145

To generate the future permalink of the post you have to use the get_sample_permalink() function:

$permalink_a = get_sample_permalink($id);

This function accepts as the first parameter the ID of the post (note that additional parameters are available, check out the WordPress documentation for more details) and returns a numeric array with at the first position the URL of the post with a placeholder instead of the post name, and at the second position the post name. The array below represents an example of the return value of this function:

[
    0 => 'https://example.com/%postname%/',
    1 => 'hello-world'
]

To get the actual permalink of the future post you have to replace the placeholder with the post name. A regular expression like the one below can be used:

$permalink = preg_replace('/\%postname\%/', $permalink_a[1], $permalink_a[0]);

Please note that since the get_sample_permalink() function is not available in certain WordPress parts, you might receive the message “Fatal error: Call to undefined function get_sample_permalink()” when the function is used. To solve simply include the file that defines the function before the first occurrence of the function:

require_once(ABSPATH . 'wp-admin/includes/post.php');

The complete code

Wouldn’t be great to have an alternative to the get_permalink() function that automatically detects if a post is a future post and in that case returns the future permalink? This is exactly what the function below does. Follow the commented parts to understand exactly all the instructions present in the code.

/**
 * Gets the permalink of the post.
 *
 * Note that if the post status is 'future' the value of the permalink field is generated with the get_sample_permalink() function.
 *
 * @param $post_id The post ID.
 * @param $require True if the wp-admin/includes/post.php file should be required.
 *
 * @return String The permalink of the post associated with the provided post ID.
 */
function get_permalink_alternative($post_id, $require = false){

    $post_status = get_post_status($post_id);

    /**
     * If the post status is 'future' the permalink is generated with the get_future_permalink() 
     * function. Otherwise it's generated with the get_permalink() function.
     */
    if($post_status === 'future'){

     if($require){
       require_once(ABSPATH . 'wp-admin/includes/post.php');
     }

     $permalink_a = get_sample_permalink($post_id);
     $permalink = preg_replace('/\%postname\%/', $permalink_a[1], $permalink_a[0]);

    }else{

      $permalink = get_permalink($post_id);

    }

 return $permalink;

}