get_page_by_title()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
get_page_by_title( string $page_title, string $output = OBJECT, string|array $post_type = 'page' ): WP_Post|array|null
Описание
Если несколько записей используют один и тот же заголовок, будет возвращена запись с наименьшим ID.
Будьте внимательны: при наличии нескольких записей с одинаковым заголовком проверяется самая ранняя дата публикации, а не наименьший ID.
Поскольку функция использует сравнение MySQL '=', $page_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.
Параметры
$page_title
string
обязательный
$output
string
необязательный
= OBJECT
$post_type
string|array
необязательный
= 'page'
Возвращаемое значение
WP_Post|array|null
Исходный код
wp-includes/deprecated.php:4573
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
_deprecated_function( __FUNCTION__, '6.2.0', 'WP_Query' );
global $wpdb;
if ( is_array( $post_type ) ) {
$post_type = esc_sql( $post_type );
$post_type_in_string = "'" . implode( "','", $post_type ) . "'";
$sql = $wpdb->prepare(
"SELECT ID
FROM $wpdb->posts
WHERE post_title = %s
AND post_type IN ($post_type_in_string)",
$page_title
);
} else {
$sql = $wpdb->prepare(
"SELECT ID
FROM $wpdb->posts
WHERE post_title = %s
AND post_type = %s",
$page_title,
$post_type
);
}
$page = $wpdb->get_var( $sql );
if ( $page ) {
return get_post( $page, $output );
}
return null;
}
История изменений
| Версия | Описание |
|---|---|
| 6.2.0 | Deprecated. Use WP_Query. |
| 3.0.0 | The $post_type parameter was added. |
| 2.1.0 | Introduced. |

