Languages: English • 日本語 (Add your language)
Retrieves a page or other post object given its path.
<?php get_page_by_path( $page_path, $output, $post_type ); ?>
This is the equivalent of the 'pagename' query, as in: 'index.php?pagename=parent-page/sub-page'.
Code for the above could be written as (assuming 'parent-page/sub-page' is actually the path to a page):
get_page_by_path('parent-page/sub-page');
For non-hierarchical custom post types, you need to use just the slug in tandem with the post_type parameter.
//Returns nothing, assumes animals is the rewrite slug for the animal CPT get_page_by_path('animals/cat', OBJECT, 'animal'); //Returns the animal with the slug 'cat' get_page_by_path('cat', OBJECT, 'animal');
The functions basename() and untrailingslashit() are handy for grabbing the last part of the URL for this:
$page_path = 'animals/cat/'; get_page_by_path( basename( untrailingslashit( $page_path ) ) , OBJECT, 'animal');
Since: 2.1.0
get_page_by_path() is located in wp-includes/post.php
.
Page Tags: get_all_page_ids(), get_ancestors(), get_page(), get_page_link(), get_page_by_path(), get_page_by_title(), get_page_children(), get_page_hierarchy(), get_page_uri(), get_pages(), is_page(), page_uri_index(), wp_list_pages(), wp_page_menu()