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.
Parameters
$page_titlestring
Page title.
$outputstringoptional
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.Default: OBJECT
$post_typestring|arrayoptional
Post type or array of post types. Default 'page'.Default: 'page'
Return
WP_Post|array|null
WP_Post (or array) on success, or null on failure.
4573functionget_page_by_title($page_title,$output=OBJECT,$post_type='page'){4574_deprecated_function(__FUNCTION__,'6.2.0','WP_Query');4575global$wpdb;45764577if(is_array($post_type)){4578$post_type=esc_sql($post_type);4579$post_type_in_string="'".implode("','",$post_type)."'";4580$sql=$wpdb->prepare(4581"SELECT ID4582 FROM $wpdb->posts4583 WHERE post_title = %s4584 AND post_type IN ($post_type_in_string)",4585$page_title4586);4587}else{4588$sql=$wpdb->prepare(4589"SELECT ID4590 FROM $wpdb->posts4591 WHERE post_title = %s4592 AND post_type = %s",4593$page_title,4594$post_type4595);4596}45974598$page=$wpdb->get_var($sql);45994600if($page){4601returnget_post($page,$output);4602}46034604returnnull;4605}
History
Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
6.2.0
Deprecated. Use WP_Query.from the docblock
3.0.0
The $post_type parameter was added.from the docblock
2.1.0
Introduced.from the docblock
About this page
Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/deprecated.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.