get_page_by_path

Retrieves a page given its path.

Description

(WP_Post|array|null) get_page_by_path( (string) $page_path, (constant) $output = OBJECT, (string) $post_type = 'page' ); 

Returns (WP_Post|array|null)

WP_Post (or array) on success, or null on failure.

Parameters (3)

0. $page_path (string)
The page path.
1. $output — Optional. (constant) => OBJECT
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
2. $post_type — Optional. (string) => 'page'
Post type or array of post types. Default page..

Usage

  1. if ( !function_exists( 'get_page_by_path' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // The page path. 
  5. $page_path = ''; 
  6.  
  7. // Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  8. // a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. 
  9. $output = OBJECT; 
  10.  
  11. // Optional. Post type or array of post types. Default 'page'. 
  12. $post_type = 'page'; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = get_page_by_path($page_path, $output, $post_type); 
  16.  

Defined (1)

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

/wp-includes/post.php  
  1. function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { 
  2. global $wpdb; 
  3.  
  4. $last_changed = wp_cache_get_last_changed( 'posts' ); 
  5.  
  6. $hash = md5( $page_path . serialize( $post_type ) ); 
  7. $cache_key = "get_page_by_path:$hash:$last_changed"; 
  8. $cached = wp_cache_get( $cache_key, 'posts' ); 
  9. if ( false !== $cached ) { 
  10. // Special case: '0' is a bad `$page_path`. 
  11. if ( '0' === $cached || 0 === $cached ) { 
  12. return; 
  13. } else { 
  14. return get_post( $cached, $output ); 
  15.  
  16. $page_path = rawurlencode(urldecode($page_path)); 
  17. $page_path = str_replace('%2F', '/', $page_path); 
  18. $page_path = str_replace('%20', ' ', $page_path); 
  19. $parts = explode( '/', trim( $page_path, '/' ) ); 
  20. $parts = esc_sql( $parts ); 
  21. $parts = array_map( 'sanitize_title_for_query', $parts ); 
  22.  
  23. $in_string = "'" . implode( "', '", $parts ) . "'"; 
  24.  
  25. if ( is_array( $post_type ) ) { 
  26. $post_types = $post_type; 
  27. } else { 
  28. $post_types = array( $post_type, 'attachment' ); 
  29.  
  30. $post_types = esc_sql( $post_types ); 
  31. $post_type_in_string = "'" . implode( "', '", $post_types ) . "'"; 
  32. $sql = " 
  33. SELECT ID, post_name, post_parent, post_type 
  34. FROM $wpdb->posts 
  35. WHERE post_name IN ($in_string) 
  36. AND post_type IN ($post_type_in_string) 
  37. "; 
  38.  
  39. $pages = $wpdb->get_results( $sql, OBJECT_K ); 
  40.  
  41. $revparts = array_reverse( $parts ); 
  42.  
  43. $foundid = 0; 
  44. foreach ( (array) $pages as $page ) { 
  45. if ( $page->post_name == $revparts[0] ) { 
  46. $count = 0; 
  47. $p = $page; 
  48.  
  49. /** 
  50. * Loop through the given path parts from right to left,  
  51. * ensuring each matches the post ancestry. 
  52. */ 
  53. while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) { 
  54. $count++; 
  55. $parent = $pages[ $p->post_parent ]; 
  56. if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) 
  57. break; 
  58. $p = $parent; 
  59.  
  60. if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) { 
  61. $foundid = $page->ID; 
  62. if ( $page->post_type == $post_type ) 
  63. break; 
  64.  
  65. // We cache misses as well as hits. 
  66. wp_cache_set( $cache_key, $foundid, 'posts' ); 
  67.  
  68. if ( $foundid ) { 
  69. return get_post( $foundid, $output );