Twenty Fourteen: optimize featured content lookup to reduce the number of queries for featured posts. Props Chouby and obenland, closes #26744.
git-svn-id: https://develop.svn.wordpress.org/trunk@29174 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
4d6759dcf5
commit
a9f6a727eb
@ -151,46 +151,39 @@ class Featured_Content {
|
|||||||
* @return array Array of post IDs.
|
* @return array Array of post IDs.
|
||||||
*/
|
*/
|
||||||
public static function get_featured_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' );
|
$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();
|
$settings = self::get_setting();
|
||||||
|
|
||||||
// Return sticky post ids if no tag name is set.
|
|
||||||
$term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
|
$term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
|
||||||
if ( $term ) {
|
|
||||||
$tag = $term->term_id;
|
|
||||||
} else {
|
|
||||||
return self::get_sticky_posts();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if ( $term ) {
|
||||||
// Query for featured posts.
|
// Query for featured posts.
|
||||||
$featured = get_posts( array(
|
$featured_ids = get_posts( array(
|
||||||
|
'fields' => 'ids',
|
||||||
'numberposts' => self::$max_posts,
|
'numberposts' => self::$max_posts,
|
||||||
|
'suppress_filters' => false,
|
||||||
'tax_query' => array(
|
'tax_query' => array(
|
||||||
array(
|
array(
|
||||||
'field' => 'term_id',
|
'field' => 'term_id',
|
||||||
'taxonomy' => 'post_tag',
|
'taxonomy' => 'post_tag',
|
||||||
'terms' => $tag,
|
'terms' => $term->term_id,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
) );
|
) );
|
||||||
|
|
||||||
// Return array with sticky posts if no Featured Content exists.
|
|
||||||
if ( ! $featured ) {
|
|
||||||
return self::get_sticky_posts();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure correct format before save/return.
|
// Get sticky posts if no Featured Content exists.
|
||||||
$featured_ids = wp_list_pluck( (array) $featured, 'ID' );
|
if ( ! $featured_ids ) {
|
||||||
$featured_ids = array_map( 'absint', $featured_ids );
|
$featured_ids = self::get_sticky_posts();
|
||||||
|
}
|
||||||
|
|
||||||
set_transient( 'featured_content_ids', $featured_ids );
|
set_transient( 'featured_content_ids', $featured_ids );
|
||||||
|
}
|
||||||
|
|
||||||
return $featured_ids;
|
// Ensure correct format before return.
|
||||||
|
return array_map( 'absint', $featured_ids );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user