get_page_by_title() – Retrieves a page given its title.

You appear to be a bot. Output may be restricted

Description

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.

Usage

$WP_Post|array|null = get_page_by_title( $page_title, $output, $post_type );

Parameters

$page_title
( string ) required – Page title.
$output
( string ) optional default: OBJECT – 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.
$post_type
( string|array ) optional default: page – Optional. Post type or array of post types. Default 'page'.

Returns

WP_Post|array|null WP_Post (or array) on success, or null on failure.

Source

File name: wordpress/wp-includes/post.php


Lines:

1 to 37 of 37
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
  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;
}
 

 View on GitHub View on Trac