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.
4574functionget_page_by_title($page_title,$output=OBJECT,$post_type='page'){4575_deprecated_function(__FUNCTION__,'6.2.0','WP_Query');4576global$wpdb;45774578if(is_array($post_type)){4579$post_type=esc_sql($post_type);4580$post_type_in_string="'".implode("','",$post_type)."'";4581$sql=$wpdb->prepare(4582"SELECT ID4583 FROM $wpdb->posts4584 WHERE post_title = %s4585 AND post_type IN ($post_type_in_string)",4586$page_title4587);4588}else{4589$sql=$wpdb->prepare(4590"SELECT ID4591 FROM $wpdb->posts4592 WHERE post_title = %s4593 AND post_type = %s",4594$page_title,4595$post_type4596);4597}45984599$page=$wpdb->get_var($sql);46004601if($page){4602returnget_post($page,$output);4603}46044605returnnull;4606}
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 6.8.8 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.