Twenty Fourteen: optimize featured content lookup to reduce the number of queries for featured posts. Props Chouby and obenland, closes #26744.

Built from https://develop.svn.wordpress.org/trunk@29174


git-svn-id: http://core.svn.wordpress.org/trunk@28958 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Lance Willett 2014-07-15 06:44:15 +00:00
parent 1ecb1f0e8b
commit f5fcb58404
2 changed files with 31 additions and 38 deletions

View File

@ -151,46 +151,39 @@ class Featured_Content {
* @return array Array of post IDs.
*/
public static function get_featured_post_ids() {
// Return array of cached results if they exist.
// Get array of cached results if they exist.
$featured_ids = get_transient( 'featured_content_ids' );
if ( ! empty( $featured_ids ) ) {
return array_map( 'absint', (array) $featured_ids );
if ( false === $featured_ids ) {
$settings = self::get_setting();
$term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
if ( $term ) {
// Query for featured posts.
$featured_ids = get_posts( array(
'fields' => 'ids',
'numberposts' => self::$max_posts,
'suppress_filters' => false,
'tax_query' => array(
array(
'field' => 'term_id',
'taxonomy' => 'post_tag',
'terms' => $term->term_id,
),
),
) );
}
// Get sticky posts if no Featured Content exists.
if ( ! $featured_ids ) {
$featured_ids = self::get_sticky_posts();
}
set_transient( 'featured_content_ids', $featured_ids );
}
$settings = self::get_setting();
// Return sticky post ids if no tag name is set.
$term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
if ( $term ) {
$tag = $term->term_id;
} else {
return self::get_sticky_posts();
}
// Query for featured posts.
$featured = get_posts( array(
'numberposts' => self::$max_posts,
'tax_query' => array(
array(
'field' => 'term_id',
'taxonomy' => 'post_tag',
'terms' => $tag,
),
),
) );
// Return array with sticky posts if no Featured Content exists.
if ( ! $featured ) {
return self::get_sticky_posts();
}
// Ensure correct format before save/return.
$featured_ids = wp_list_pluck( (array) $featured, 'ID' );
$featured_ids = array_map( 'absint', $featured_ids );
set_transient( 'featured_content_ids', $featured_ids );
return $featured_ids;
// Ensure correct format before return.
return array_map( 'absint', $featured_ids );
}
/**

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.0-beta1-20140714';
$wp_version = '4.0-beta1-20140715';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.