Advertisements

get_page_by_path

Definition:
function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {}

Retrieves a page given its path.

Parameters

  • string $page_path: Page path
  • string $output: Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
  • string $post_type: Optional. Post type. Default page.

Return values

returns:Null when complete.

Source code

function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {

	global $wpdb;



	$page_path = rawurlencode(urldecode($page_path));

	$page_path = str_replace('%2F', '/', $page_path);

	$page_path = str_replace('%20', ' ', $page_path);

	$parts = explode( '/', trim( $page_path, '/' ) );

	$parts = array_map( 'esc_sql', $parts );

	$parts = array_map( 'sanitize_title_for_query', $parts );



	$in_string = "'". implode( "','", $parts ) . "'";

	$post_type_sql = $post_type;

	$wpdb->escape_by_ref( $post_type_sql );

	$pages = $wpdb->get_results( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name IN ($in_string) AND (post_type = '$post_type_sql' OR post_type = 'attachment')", OBJECT_K );



	$revparts = array_reverse( $parts );



	$foundid = 0;

	foreach ( (array) $pages as $page ) {

		if ( $page->post_name == $revparts[0] ) {

			$count = 0;

			$p = $page;

			while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) {

				$count++;

				$parent = $pages[ $p->post_parent ];

				if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] )

					break;

				$p = $parent;

			}



			if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) {

				$foundid = $page->ID;

				break;

			}

		}

	}



	if ( $foundid )

		return get_page( $foundid, $output );



	return null;

}

1530

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: