The get_preview_post_link() WordPress PHP function retrieves the URL used for the post preview.
Usage
get_preview_post_link( $post, $query_args, $preview_link );
Parameters
$post
(int|WP_Post) (Optional) – Post ID or WP_Post object. Defaults to the global$post
. Default:null
$query_args
(array) (Optional) – Array of additional query args to be appended to the link. Default:array()
$preview_link
(string) (Optional) – Base preview link to be used if it should differ from the post permalink. Default:''
More information
See WordPress Developer Resources: get_preview_post_link()
Examples
Basic usage
This example shows how to get the preview post link for the current post in the loop.
$preview_link = get_preview_post_link(); echo 'Preview link: ' . $preview_link;
Specify a custom post
This example demonstrates how to get the preview post link for a specific post with ID 42
.
$post_id = 42; $preview_link = get_preview_post_link( $post_id ); echo 'Preview link: ' . $preview_link;
Add custom query args
In this example, we add a custom query argument (my_arg
with value example_value
) to the preview post link.
$query_args = array( 'my_arg' => 'example_value' ); $preview_link = get_preview_post_link( null, $query_args ); echo 'Preview link: ' . $preview_link;
Use a custom preview link
This example shows how to use a custom base preview link for the current post.
$custom_preview_link = 'https://example.com/custom-preview/'; $preview_link = get_preview_post_link( null, array(), $custom_preview_link ); echo 'Preview link: ' . $preview_link;
Combining custom post, query args, and preview link
This example demonstrates how to get the preview post link for a specific post with ID 42
, adding custom query arguments and using a custom base preview link.
$post_id = 42; $query_args = array( 'my_arg' => 'example_value' ); $custom_preview_link = 'https://example.com/custom-preview/'; $preview_link = get_preview_post_link( $post_id, $query_args, $custom_preview_link ); echo 'Preview link: ' . $preview_link;