wpseek.com
A WordPress-centric search engine for devs and theme authors



get_page_by_title › WordPress Function

Since2.1.0
Deprecatedn/a
get_page_by_title ( $page_title, $output = OBJECT, $post_type = 'page' )
Parameters: (3)
  • (string) $page_title Page title.
    Required: Yes
  • (string) $output Optional. 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.
    Required: No
    Default: OBJECT
  • (string|array) $post_type Optional. Post type or array of post types. Default 'page'.
    Required: No
    Default: 'page'
Returns:
  • (WP_Post|array|null) WP_Post (or array) on success, or null on failure.
Defined at:
Codex:
Change Log:
  • 3.0.0

Retrieves a page given its title.

If more than one post uses the same title, the post with the smallest ID will be returned. Be careful: in case of more than one post having the same title, it will check the oldest publication date, not the smallest ID. Because this function uses the MySQL '=' comparison, $page_title will usually be matched as case-insensitive with default collation.


Source

function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
	$args  = array(
		'title'                  => $page_title,
		'post_type'              => $post_type,
		'post_status'            => get_post_stati(),
		'posts_per_page'         => 1,
		'update_post_term_cache' => false,
		'update_post_meta_cache' => false,
		'no_found_rows'          => true,
		'orderby'                => 'post_date ID',
		'order'                  => 'ASC',
	);
	$query = new WP_Query( $args );
	$pages = $query->posts;

	if ( empty( $pages ) ) {
		return null;
	}

	return get_post( $pages[0], $output );
}