get_permalink( int|WP_Post $post = 0, bool $leavename = false ): string|false
- Since
- 1.0.0
- Source
wp-includes/link-template.php:170
Builds the front-end URL for a post, page, or attachment based on the site's permalink structure and post type. It accepts a post ID or WP_Post object and falls back to the current global $post when no argument is given, returning false if the post cannot be found. Because the raw URL isn't run through display escaping, wrap it in esc_url() or use the_permalink() when printing it directly in a template.
Compatibility
- WordPress
- since 1.0.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 post object. Default is the global
$post.Default:0 $leavenamebooloptional- Whether to keep post name or page name. Default false.Default:
false
Return value
string|false- The permalink URL. False if the post does not exist.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Print the permalink for each post in a custom loop
Loop over the seeded posts and show the title next to its computed permalink.
$post_ids = array( 1, 2, 3, 4, 5 );
foreach ( $post_ids as $post_id ) {
$post = get_post( $post_id );
if ( ! $post ) {
continue;
}
$permalink = get_permalink( $post_id );
if ( $permalink ) {
echo esc_html( $post->post_title ) . ': ' . esc_html( $permalink ) . "\n";
} else {
echo esc_html( $post->post_title ) . ': no permalink available' . "\n";
}
}get_post() is called first so the loop can skip gracefully if an ID has been deleted, since get_permalink() itself would just return false.
Compare a filled-in permalink with the raw %postname% template
Switch on a pretty permalink structure and pass $leavename to see the unreplaced token, a technique similar to what the slug editor on the edit-post screen relies on.
$post = get_post( 2 );
$resolved = get_permalink( $post );
$template = get_permalink( $post, true );
printf( "Resolved: %s\n", esc_html( $resolved ) );
printf( "Template: %s\n", esc_html( $template ) );With the default plain permalink structure the two calls return an identical ?p= style link, since there is no %postname% token to leave in place.
Common problems and fixes · 4
- Why does get_permalink() return false?
- Why does get_permalink() keep returning a ?p=123 link instead of a pretty URL?
- Why does %category% in my permalink structure only show one term when a post has two categories?
- Do I need to escape the URL get_permalink() returns before echoing it?
Why does get_permalink() return false?
Why does get_permalink() keep returning a ?p=123 link instead of a pretty URL?
Why does %category% in my permalink structure only show one term when a post has two categories?
Do I need to escape the URL get_permalink() returns before echoing it?
Alternatives and related functions
get_the_permalink- When you're already inside The Loop and want the result passed through the 'the_permalink' filter that themes and plugins commonly hook into.
the_permalink- When you just want to echo the current post's permalink directly in a template without capturing it in a variable first.
get_post_permalink- When you specifically need the link for a custom post type and want to bypass the post_type-detection branching that get_permalink() does internally.
get_page_link- When you know in advance you're linking to a page rather than a post, so you can skip the post-type check get_permalink() performs before delegating to it.
Performance profile
How much work a call to get_permalink() 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
- 28–182
- Plugin surface
- 3 hooks
- Called by
- 50
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 210.
Third-party callbacks on 'pre_post_link', 'post_link_category', 'post_link' run inside this call, and their cost is not bounded by anything here.
50 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 - optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below get_permalink() - serializeserialisation
maybe_unserialize()one call below get_permalink()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 52 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_permalink() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($post) | 28–35 | get_post() |
is_object($post) && isset($post) && empty($post) | 30–32 | none |
!empty($post) | 36–43 | get_post(), get_page_link() |
is_object($post) && isset($post) && !empty($post) | 38–40 | get_page_link() |
!empty($post) | 38–45 | get_post(), get_attachment_link() |
is_object($post) && isset($post) && !empty($post) | 40–42 | get_attachment_link() |
!empty($post) | 46–53 | get_post(), get_post_types(), get_post_permalink() |
is_object($post) && isset($post) && !empty($post) | 48–50 | get_post_types(), get_post_permalink() |
!empty($post) | 65–72 | get_post(), get_post_types(), get_option(), apply_filters(), home_url(), apply_filters() |
is_object($post) && isset($post) && !empty($post) | 67–69 | get_post_types(), get_option(), apply_filters(), home_url(), apply_filters() |
!empty($post) && wp_force_plain_post_permalink() | 69–76 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), home_url(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && wp_force_plain_post_permalink() | 71–73 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), home_url(), apply_filters() |
40 further outcomes, up to 179 instructions
!empty($post) && !wp_force_plain_post_permalink() && !$permalink | 110–117 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && !$permalink | 112–114 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() | 118–125 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && !empty($category) | 119–126 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() | 120–122 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && !empty($category) | 121–123 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $permalink && !empty($category) | 127–134 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && empty($category) | 128–135 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), get_option(), get_term(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $permalink && !empty($category) | 129–131 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && empty($category) | 130–132 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), get_option(), get_term(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && empty($category) | 132–141 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), get_option(), get_term(), is_wp_error(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && empty($category) | 134–138 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), get_option(), get_term(), is_wp_error(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $permalink && empty($category) | 136–143 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), get_option(), get_term(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $permalink && empty($category) | 138–140 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), get_option(), get_term(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $permalink && empty($category) | 140–149 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), get_option(), get_term(), is_wp_error(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && !$category_object && !empty($category) | 142–149 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $permalink && empty($category) | 142–146 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), get_option(), get_term(), is_wp_error(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && !$category_object && !empty($category) | 144–146 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $permalink && !$category_object && !empty($category) | 150–157 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && !$category_object && empty($category) | 151–158 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_option(), get_term(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $permalink && !$category_object && !empty($category) | 152–154 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $category_object && !empty($category) | 152–159 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && !$category_object && empty($category) | 153–155 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_option(), get_term(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $category_object && !empty($category) | 154–156 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && !$category_object && empty($category) | 155–164 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_option(), get_term(), is_wp_error(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && !$category_object && empty($category) | 157–161 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_option(), get_term(), is_wp_error(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $permalink && !$category_object && empty($category) | 159–166 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_option(), get_term(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $permalink && $category_object && !empty($category) | 160–167 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $permalink && !$category_object && empty($category) | 161–163 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_option(), get_term(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $category_object && empty($category) | 161–168 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), get_option(), get_term(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $permalink && $category_object && !empty($category) | 162–164 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $category_object && empty($category) | 163–165 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), get_option(), get_term(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $permalink && !$category_object && empty($category) | 163–172 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_option(), get_term(), is_wp_error(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $permalink && !$category_object && empty($category) | 165–169 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_option(), get_term(), is_wp_error(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $category_object && empty($category) | 165–174 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), get_option(), get_term(), is_wp_error(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $category_object && empty($category) | 167–171 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), get_option(), get_term(), is_wp_error(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $permalink && $category_object && empty($category) | 169–176 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), get_option(), get_term(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $permalink && $category_object && empty($category) | 171–173 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), get_option(), get_term(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
!empty($post) && !wp_force_plain_post_permalink() && $permalink && $category_object && empty($category) | 173–182 | get_post(), get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), get_option(), get_term(), is_wp_error(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
is_object($post) && isset($post) && !empty($post) && !wp_force_plain_post_permalink() && $permalink && $category_object && empty($category) | 175–179 | get_post_types(), get_option(), apply_filters(), wp_force_plain_post_permalink(), get_the_category(), wp_list_sort(), apply_filters(), get_term(), get_category_parents(), get_option(), get_term(), is_wp_error(), get_userdata(), explode(), home_url(), user_trailingslashit(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 210 | 28–182 | 18 | |
| 8.5 | 210 | 28–182 | 18 | |
| 8.4 | 210 | 28–182 | 18 | 15 fewer instructions than PHP 8.3 |
| 8.3 | 225 | 28–197 | 18 | |
| 8.2 | 225 | 28–197 | 18 | |
| 8.1 | 225 | 28–197 | 18 | |
| 7.4 | 225 | 28–197 | 18 |
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 · 3
3 hooks fire while get_permalink() runs, in this order:
- apply_filters( pre_post_link )filterline 217 (+47 into the body)
Filters the permalink structure for a post before token replacement occurs.
- apply_filters( post_link_category )filterline 244 (+74 into the body)
Filters the category that gets used in the %category% permalink token.
Uses · 17
- get_post()Retrieves post data given a post ID or post object.
- get_page_link()Retrieves the permalink for the current page or page ID.
- get_attachment_link()Retrieves the permalink for an attachment.
- get_post_types()Gets a list of all registered post type objects.
- get_post_permalink()Retrieves the permalink for a post of a custom post type.
- get_option()Retrieves an option value based on an option name.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_force_plain_post_permalink()Determine whether post should always use a plain permalink structure.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- get_the_category()Retrieves post categories.
- wp_list_sort()Sorts an array of objects or arrays based on one or more orderby arguments.
- get_term()Gets all term data from database by term ID.
Show all 17
- get_category_parents()Retrieves category parents with separator.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- get_userdata()Retrieves user info by user ID.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- user_trailingslashit()Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
Used by · 50
- TwentyTwenty_Walker_Page::start_el()Outputs the beginning of the current element in the tree.
- Twenty_Eleven_Ephemera_Widget::widget()Outputs the HTML for this widget.
- Twenty_Fourteen_Ephemera_Widget::widget()Outputs the HTML for this widget.
- WP_Comments_List_Table::column_response()Outputs the response column.
- WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()Get the value emulated into a WP_Post and set up as a nav_menu_item.
- WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()Ajax handler for adding a new auto-draft post.
- WP_Customize_Nav_Menus::load_available_items_query()Performs the post_type and taxonomy queries for loading available menu items.
- WP_Customize_Nav_Menus::search_available_items_query()Performs post queries for available-item searching.
- WP_Media_List_Table::_get_row_actions()Gets the row actions for a media item.
- WP_Posts_List_Table::handle_row_actions()Generates and displays row action links.
- WP_REST_Post_Search_Handler::prepare_item()Prepares the search result for a given post ID.
- WP_REST_Posts_Controller::get_item()Retrieves a single post.
Show all 50
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
- WP_Sitemaps_Posts::get_url_list()Gets a URL list for a post type sitemap.
- Walker_Page::start_el()Outputs the beginning of the current element in the tree.
- _WP_Editors::wp_link_query()Performs post queries for internal linking.
- _block_bindings_post_data_get_value()Gets value for Post Data source.
- _transition_post_status()Hook for managing future post transitions to published.
- _wp_link_page()Helper function for wp_link_pages().
- block_core_breadcrumbs_get_hierarchical_post_type_breadcrumbs()Generates breadcrumb items from hierarchical post type ancestors.
- comment_form()Outputs a complete commenting form for use within a template.
- comments_popup_link()Displays the link to the comments for the current post ID.
- get_adjacent_post_link()Retrieves the adjacent post link.
- get_adjacent_post_rel_link()Retrieves the adjacent post relational link.
- get_attachment_link()Retrieves the permalink for an attachment.
- get_blog_permalink()Gets the permalink for a post on another blog.
- get_boundary_post_rel_link()Get boundary post relational link.
- get_comment_link()Retrieves the link to a given comment.
- get_comment_reply_link()Retrieves HTML content for reply to comment link.
- get_comments_link()Retrieves the link to the current post comments.
- get_comments_pagenum_link()Retrieves the comments page number link.
- get_media_item()Retrieves HTML form for modifying the image attachment.
- get_parent_post_rel_link()Get parent post relational link.
- get_post_comments_feed_link()Retrieves the permalink for the post comments feed.
- get_post_embed_html()Retrieves the embed code for a specific post.
- get_post_embed_url()Retrieves the URL to embed a specific post in an iframe.
- get_post_reply_link()Retrieves HTML content for reply to post link.
- get_post_type_archive_link()Retrieves the permalink for a post type archive.
- get_preview_post_link()Retrieves the URL used for the post preview.
- get_privacy_policy_url()Retrieves the URL to the privacy policy page.
- get_sample_permalink()Returns a sample permalink based on the post name.
- get_sample_permalink_html()Returns the HTML of the sample permalink slug editor.
- get_the_content()Retrieves the post content.
- get_the_password_form()Retrieves protected post password form content.
- get_the_permalink()Retrieves the full permalink for the current post or post ID.
- get_trackback_url()Retrieves the current post's trackback URL.
- next_post()Prints link to the next post.
- paginate_comments_links()Displays or retrieves pagination links for the comments on the current post.
- pingback()Pings back the links found in a post.
- post_permalink()Retrieve permalink from post ID.
Source code
function get_permalink( $post = 0, $leavename = false ) { $rewritecode = array( '%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', $leavename ? '' : '%postname%', '%post_id%', '%category%', '%author%', $leavename ? '' : '%pagename%', ); if ( is_object( $post ) && isset( $post->filter ) && 'sample' === $post->filter ) { $sample = true; } else { $post = get_post( $post ); $sample = false; } if ( empty( $post->ID ) ) { return false; } if ( 'page' === $post->post_type ) { return get_page_link( $post, $leavename, $sample ); } elseif ( 'attachment' === $post->post_type ) { return get_attachment_link( $post, $leavename ); } elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ), true ) ) { return get_post_permalink( $post, $leavename, $sample ); } $permalink = get_option( 'permalink_structure' ); /** * Filters the permalink structure for a post before token replacement occurs. * * Only applies to posts with post_type of 'post'. * * @since 3.0.0 * * @param string $permalink The site's permalink structure. * @param WP_Post $post The post in question. * @param bool $leavename Whether to keep the post name. */ $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename ); if ( $permalink && ! wp_force_plain_post_permalink( $post ) ) { $category = ''; if ( str_contains( $permalink, '%category%' ) ) { $cats = get_the_category( $post->ID ); if ( $cats ) { $cats = wp_list_sort( $cats, array( 'term_id' => 'ASC', ) ); /** * Filters the category that gets used in the %category% permalink token. * * @since 3.5.0 * * @param WP_Term $cat The category to use in the permalink. * @param array $cats Array of all categories (WP_Term objects) associated with the post. * @param WP_Post $post The post in question. */ $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post ); $category_object = get_term( $category_object, 'category' ); $category = $category_object->slug; if ( $category_object->parent ) { $category = get_category_parents( $category_object->parent, false, '/', true ) . $category;Changelog
Introduced in 1.0.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.