get_page_by_title

The timeline below displays how wordpress function get_page_by_title has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * 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.
 *
 * @since 2.1.0
 * @since 3.0.0 The `$post_type` parameter was added.
 * @deprecated 6.2.0 Use WP_Query.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string       $page_title Page title.
 * @param 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.
 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
 */
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\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type IN ({$post_type_in_string})", $page_title);
    } else {
        $sql = $wpdb->prepare("SELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type = %s", $page_title, $post_type);
    }
    $page = $wpdb->get_var($sql);
    if ($page) {
        return get_post($page, $output);
    }
    return null;
}

WordPress Version: 6.2

/**
 * 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.
 *
 * @since 2.1.0
 * @since 3.0.0 The `$post_type` parameter was added.
 * @deprecated 6.2.0 Use WP_Query.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string       $page_title Page title.
 * @param 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.
 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
 */
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("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type IN ({$post_type_in_string})\n\t\t", $page_title);
    } else {
        $sql = $wpdb->prepare("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type = %s\n\t\t", $page_title, $post_type);
    }
    $page = $wpdb->get_var($sql);
    if ($page) {
        return get_post($page, $output);
    }
    return null;
}

WordPress Version: 1.1

/**
 * 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.
 *
 * @since 2.1.0
 * @since 3.0.0 The `$post_type` parameter was added.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string       $page_title Page title.
 * @param 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.
 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
 */
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("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type IN ({$post_type_in_string})\n\t\t", $page_title);
    } else {
        $sql = $wpdb->prepare("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type = %s\n\t\t", $page_title, $post_type);
    }
    $page = $wpdb->get_var($sql);
    if ($page) {
        return get_post($page, $output);
    }
    return null;
}

WordPress Version: 6.1

/**
 * 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.
 *
 * @since 2.1.0
 * @since 3.0.0 The `$post_type` parameter was added.
 *
 * @param string       $page_title Page title.
 * @param 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.
 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
 */
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);
}

WordPress Version: 5.9

/**
 * Retrieve 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.
 *
 * @since 2.1.0
 * @since 3.0.0 The `$post_type` parameter was added.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string       $page_title Page title.
 * @param 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.
 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
 */
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("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type IN ({$post_type_in_string})\n\t\t", $page_title);
    } else {
        $sql = $wpdb->prepare("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type = %s\n\t\t", $page_title, $post_type);
    }
    $page = $wpdb->get_var($sql);
    if ($page) {
        return get_post($page, $output);
    }
    return null;
}

WordPress Version: 5.5

/**
 * Retrieve 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.
 *
 * @since 2.1.0
 * @since 3.0.0 The `$post_type` parameter was added.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string       $page_title Page title.
 * @param 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.
 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
 */
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("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type IN ({$post_type_in_string})\n\t\t", $page_title);
    } else {
        $sql = $wpdb->prepare("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type = %s\n\t\t", $page_title, $post_type);
    }
    $page = $wpdb->get_var($sql);
    if ($page) {
        return get_post($page, $output);
    }
}

WordPress Version: 5.3

/**
 * Retrieve 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.
 *
 * @since 2.1.0
 * @since 3.0.0 The `$post_type` parameter was added.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string       $page_title Page title.
 * @param 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.
 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
 */
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("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type IN ({$post_type_in_string})\n\t\t", $page_title);
    } else {
        $sql = $wpdb->prepare("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type = %s\n\t\t", $page_title, $post_type);
    }
    $page = $wpdb->get_var($sql);
    if ($page) {
        return get_post($page, $output);
    }
}

WordPress Version: 4.7

/**
 * Retrieve a page given its title.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string       $page_title Page title
 * @param 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.
 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
 */
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("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type IN ({$post_type_in_string})\n\t\t", $page_title);
    } else {
        $sql = $wpdb->prepare("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type = %s\n\t\t", $page_title, $post_type);
    }
    $page = $wpdb->get_var($sql);
    if ($page) {
        return get_post($page, $output);
    }
}

WordPress Version: 4.3

/**
 * Retrieve a page given its title.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string       $page_title Page title
 * @param string       $output     Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
 *                                 Default OBJECT.
 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
 * @return WP_Post|array|void WP_Post on success or null on failure
 */
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("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type IN ({$post_type_in_string})\n\t\t", $page_title);
    } else {
        $sql = $wpdb->prepare("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type = %s\n\t\t", $page_title, $post_type);
    }
    $page = $wpdb->get_var($sql);
    if ($page) {
        return get_post($page, $output);
    }
}

WordPress Version: 4.0

/**
 * Retrieve a page given its title.
 *
 * @since 2.1.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string       $page_title Page title
 * @param string       $output     Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A.
 *                                 Default OBJECT.
 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
 * @return WP_Post|null WP_Post on success or null on failure
 */
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("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type IN ({$post_type_in_string})\n\t\t", $page_title);
    } else {
        $sql = $wpdb->prepare("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type = %s\n\t\t", $page_title, $post_type);
    }
    $page = $wpdb->get_var($sql);
    if ($page) {
        return get_post($page, $output);
    }
    return null;
}

WordPress Version: 3.9

/**
 * Retrieve a page given its title.
 *
 * @since 2.1.0
 * @uses $wpdb
 *
 * @param string $page_title Page title
 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
 * @param string|array $post_type Optional. Post type or array of post types. Default page.
 * @return WP_Post|null WP_Post on success or null on failure
 */
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("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type IN ({$post_type_in_string})\n\t\t", $page_title);
    } else {
        $sql = $wpdb->prepare("\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->posts}\n\t\t\tWHERE post_title = %s\n\t\t\tAND post_type = %s\n\t\t", $page_title, $post_type);
    }
    $page = $wpdb->get_var($sql);
    if ($page) {
        return get_post($page, $output);
    }
    return null;
}

WordPress Version: 3.7

/**
 * Retrieve a page given its title.
 *
 * @since 2.1.0
 * @uses $wpdb
 *
 * @param string $page_title Page title
 * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
 * @param string $post_type Optional. Post type. Default page.
 * @return WP_Post|null WP_Post on success or null on failure
 */
function get_page_by_title($page_title, $output = OBJECT, $post_type = 'page')
{
    global $wpdb;
    $page = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type= %s", $page_title, $post_type));
    if ($page) {
        return get_post($page, $output);
    }
    return null;
}