get_permalink( int|WP_Post $post = 0, bool $leavename = false ): string|false
- Since
- 1.0.0
- Source
wp-includes/link-template.php:170
Retrieves the full permalink for the current post or post ID.
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
string|false- The permalink URL. False if the post does not exist.
Hooks 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()
- 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()
- 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().
- 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.
- previous_post()Prints a link to the previous post.
Source
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;History
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 6.9.7 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.