mirror of
https://github.com/WordPress/WordPress.git
synced 2024-09-29 23:57:41 +02:00
Posts, Post types: Cache get_page_by_title()
.
Convert `get_page_by_title()` to a `WP_Query` wrapper to take advantage of the object caching built in to the latter. Add dedicated unit tests for the function `get_page_by_title()`. Props spacedmonkey, boonebgorges, igmoweb, pcfreak30, pbearne. Fixes #36905. Built from https://develop.svn.wordpress.org/trunk@54234 git-svn-id: http://core.svn.wordpress.org/trunk@53793 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
526dba25c2
commit
4f748ac3fa
@ -5756,8 +5756,6 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
|
|||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
* @since 3.0.0 The `$post_type` parameter was added.
|
* @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 $page_title Page title.
|
||||||
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
|
* @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,
|
* correspond to a WP_Post object, an associative array, or a numeric array,
|
||||||
@ -5766,40 +5764,25 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
|
|||||||
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
|
* @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' ) {
|
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
|
||||||
global $wpdb;
|
$args = array(
|
||||||
|
'post_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' => 'ID',
|
||||||
|
'order' => 'ASC',
|
||||||
|
);
|
||||||
|
$query = new WP_Query( $args );
|
||||||
|
$pages = $query->get_posts();
|
||||||
|
|
||||||
if ( is_array( $post_type ) ) {
|
if ( empty( $pages ) ) {
|
||||||
$post_type = esc_sql( $post_type );
|
return null;
|
||||||
$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 );
|
return get_post( $pages[0], $output );
|
||||||
|
|
||||||
if ( $page ) {
|
|
||||||
return get_post( $page, $output );
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '6.1-alpha-54233';
|
$wp_version = '6.1-alpha-54234';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
Loading…
Reference in New Issue
Block a user