get_permalink

Retrieves the full permalink for the current post or post ID.

Description

(string|false) get_permalink( (int) $post = 0, (bool) $leavename = false ); 

Returns (string|false)

The permalink URL or false if post does not exist.

Parameters (2)

0. $post — Optional. (int)
Post ID or post object. Default is the global $post..
1. $leavename — Optional. (bool) => false
Whether to keep post name or page name. Default false.

Usage

  1. if ( !function_exists( 'get_permalink' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. Post ID or post object. Default is the global `$post`. 
  5. $post = -1; 
  6.  
  7. // Optional. Whether to keep post name or page name. Default false. 
  8. $leavename = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_permalink($post, $leavename); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/link-template.php  
  1. function get_permalink( $post = 0, $leavename = false ) { 
  2. $rewritecode = array( 
  3. '%year%',  
  4. '%monthnum%',  
  5. '%day%',  
  6. '%hour%',  
  7. '%minute%',  
  8. '%second%',  
  9. $leavename? '' : '%postname%',  
  10. '%post_id%',  
  11. '%category%',  
  12. '%author%',  
  13. $leavename? '' : '%pagename%',  
  14. ); 
  15.  
  16. if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) { 
  17. $sample = true; 
  18. } else { 
  19. $post = get_post( $post ); 
  20. $sample = false; 
  21.  
  22. if ( empty($post->ID) ) 
  23. return false; 
  24.  
  25. if ( $post->post_type == 'page' ) 
  26. return get_page_link($post, $leavename, $sample); 
  27. elseif ( $post->post_type == 'attachment' ) 
  28. return get_attachment_link( $post, $leavename ); 
  29. elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) ) 
  30. return get_post_permalink($post, $leavename, $sample); 
  31.  
  32. $permalink = get_option('permalink_structure'); 
  33.  
  34. /** 
  35. * Filters the permalink structure for a post before token replacement occurs. 
  36. * Only applies to posts with post_type of 'post'. 
  37. * @since 3.0.0 
  38. * @param string $permalink The site's permalink structure. 
  39. * @param WP_Post $post The post in question. 
  40. * @param bool $leavename Whether to keep the post name. 
  41. */ 
  42. $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename ); 
  43.  
  44. if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) { 
  45. $unixtime = strtotime($post->post_date); 
  46.  
  47. $category = ''; 
  48. if ( strpos($permalink, '%category%') !== false ) { 
  49. $cats = get_the_category($post->ID); 
  50. if ( $cats ) { 
  51. $cats = wp_list_sort( $cats, array( 
  52. 'term_id' => 'ASC',  
  53. ) ); 
  54.  
  55. /** 
  56. * Filters the category that gets used in the %category% permalink token. 
  57. * @since 3.5.0 
  58. * @param WP_Term $cat The category to use in the permalink. 
  59. * @param array $cats Array of all categories (WP_Term objects) associated with the post. 
  60. * @param WP_Post $post The post in question. 
  61. */ 
  62. $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post ); 
  63.  
  64. $category_object = get_term( $category_object, 'category' ); 
  65. $category = $category_object->slug; 
  66. if ( $parent = $category_object->parent ) 
  67. $category = get_category_parents($parent, false, '/', true) . $category; 
  68. // show default category in permalinks, without 
  69. // having to assign it explicitly 
  70. if ( empty($category) ) { 
  71. $default_category = get_term( get_option( 'default_category' ), 'category' ); 
  72. if ( $default_category && ! is_wp_error( $default_category ) ) { 
  73. $category = $default_category->slug; 
  74.  
  75. $author = ''; 
  76. if ( strpos($permalink, '%author%') !== false ) { 
  77. $authordata = get_userdata($post->post_author); 
  78. $author = $authordata->user_nicename; 
  79.  
  80. $date = explode(" ", date('Y m d H i s', $unixtime)); 
  81. $rewritereplace = 
  82. array( 
  83. $date[0],  
  84. $date[1],  
  85. $date[2],  
  86. $date[3],  
  87. $date[4],  
  88. $date[5],  
  89. $post->post_name,  
  90. $post->ID,  
  91. $category,  
  92. $author,  
  93. $post->post_name,  
  94. ); 
  95. $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) ); 
  96. $permalink = user_trailingslashit($permalink, 'single'); 
  97. } else { // if they're not using the fancy permalink option 
  98. $permalink = home_url('?p=' . $post->ID); 
  99.  
  100. /** 
  101. * Filters the permalink for a post. 
  102. * Only applies to posts with post_type of 'post'. 
  103. * @since 1.5.0 
  104. * @param string $permalink The post's permalink. 
  105. * @param WP_Post $post The post in question. 
  106. * @param bool $leavename Whether to keep the post name. 
  107. */ 
  108. return apply_filters( 'post_link', $permalink, $post, $leavename );