get_preview_post_link( int|WP_Post $post = null, array $query_args = array(), string $preview_link = '' ): string|null
- Since
- 4.4.0
- Source
wp-includes/link-template.php:1408
Description
Allows additional query args to be appended.
Compatibility
- WordPress
- since 4.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$postint|WP_Postoptional- Post ID or
WP_Postobject. Defaults to global$post.Default:null $query_argsarrayoptional- Array of additional query args to be appended to the link.
Default empty array.Default:array() $preview_linkstringoptional- Base preview link to be used if it should differ from the post permalink. Default empty.Default:
''
Return value
string|null- URL used for the post preview, or null if the post does not exist.
Performance profile
How much work a call to get_preview_post_link() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 9–39
- Plugin surface
- 1 hook
- Called by
- 9
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 40.
Third-party callbacks on 'preview_post_link' run inside this call, and their cost is not bounded by anything here.
9 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below get_preview_post_link()
Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_preview_post_link() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 9 | get_post() |
!is_post_type_viewable() | 24 | get_post(), get_post_type_object(), is_post_type_viewable(), apply_filters() |
is_post_type_viewable() | 32 | get_post(), get_post_type_object(), is_post_type_viewable(), add_query_arg(), apply_filters() |
is_post_type_viewable() | 39 | get_post(), get_post_type_object(), is_post_type_viewable(), get_permalink(), set_url_scheme(), add_query_arg(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 40 instructions, 9–39 executed per call, 3 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while get_preview_post_link() runs, in this order:
- apply_filters( preview_post_link )filterline 1434 (+26 into the body)
Filters the URL used for a post preview.
Uses · 7
- get_post()Retrieves post data given a post ID or post object.
- get_post_type_object()Retrieves a post type object by name.
- is_post_type_viewable()Determines whether a post type is considered "viewable".
- set_url_scheme()Sets the scheme for a URL.
- get_permalink()Retrieves the full permalink for the current post or post ID.
- add_query_arg()Retrieves a modified URL query string.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 9
- WP_Posts_List_Table::handle_row_actions()Generates and displays row action links.
- WP_REST_Autosaves_Controller::prepare_item_for_response()Prepares the revision for the REST response.
- _admin_notice_post_locked()Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
- _wp_link_page()Helper function for wp_link_pages().
- get_sample_permalink_html()Returns the HTML of the sample permalink slug editor.
- post_preview()Saves a draft or manually autosaves for the purpose of showing a post preview.
- post_submit_meta_box()Displays post submit form fields.
- wp_admin_bar_edit_menu()Provides an edit link for posts and terms.
- wp_ajax_get_permalink()Handles retrieving a permalink via AJAX.
Source code
function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) { $post = get_post( $post ); if ( ! $post ) { return null; } $post_type_object = get_post_type_object( $post->post_type ); if ( is_post_type_viewable( $post_type_object ) ) { if ( ! $preview_link ) { $preview_link = set_url_scheme( get_permalink( $post ) ); } $query_args['preview'] = 'true'; $preview_link = add_query_arg( $query_args, $preview_link ); } /** * Filters the URL used for a post preview. * * @since 2.0.5 * @since 4.0.0 Added the `$post` parameter. * * @param string $preview_link URL used for the post preview. * @param WP_Post $post Post object. */ return apply_filters( 'preview_post_link', $preview_link, $post );}Changelog
Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/link-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.