Make WordPress Core

Changeset 29174


Ignore:
Timestamp:
07/15/2014 06:43:31 AM (12 years ago)
Author:
lancewillett
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentyfourteen/inc/featured-content.php

    r28536 r29174  
    152152         */
    153153        public static function get_featured_post_ids() {
    154                 // Return array of cached results if they exist.
     154                // Get array of cached results if they exist.
    155155                $featured_ids = get_transient( 'featured_content_ids' );
    156                 if ( ! empty( $featured_ids ) ) {
    157                         return array_map( 'absint', (array) $featured_ids );
    158                 }
    159 
    160                 $settings = self::get_setting();
    161 
    162                 // Return sticky post ids if no tag name is set.
    163                 $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
    164                 if ( $term ) {
    165                         $tag = $term->term_id;
    166                 } else {
    167                         return self::get_sticky_posts();
    168                 }
    169 
    170                 // Query for featured posts.
    171                 $featured = get_posts( array(
    172                         'numberposts' => self::$max_posts,
    173                         'tax_query'   => array(
    174                                 array(
    175                                         'field'    => 'term_id',
    176                                         'taxonomy' => 'post_tag',
    177                                         'terms'    => $tag,
    178                                 ),
    179                         ),
    180                 ) );
    181 
    182                 // Return array with sticky posts if no Featured Content exists.
    183                 if ( ! $featured ) {
    184                         return self::get_sticky_posts();
    185                 }
    186 
    187                 // Ensure correct format before save/return.
    188                 $featured_ids = wp_list_pluck( (array) $featured, 'ID' );
    189                 $featured_ids = array_map( 'absint', $featured_ids );
    190 
    191                 set_transient( 'featured_content_ids', $featured_ids );
    192 
    193                 return $featured_ids;
     156
     157                if ( false === $featured_ids ) {
     158                        $settings = self::get_setting();
     159                        $term     = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
     160
     161                        if ( $term ) {
     162                                // Query for featured posts.
     163                                $featured_ids = get_posts( array(
     164                                        'fields'           => 'ids',
     165                                        'numberposts'      => self::$max_posts,
     166                                        'suppress_filters' => false,
     167                                        'tax_query'        => array(
     168                                                array(
     169                                                        'field'    => 'term_id',
     170                                                        'taxonomy' => 'post_tag',
     171                                                        'terms'    => $term->term_id,
     172                                                ),
     173                                        ),
     174                                ) );
     175                        }
     176
     177                        // Get sticky posts if no Featured Content exists.
     178                        if ( ! $featured_ids ) {
     179                                $featured_ids = self::get_sticky_posts();
     180                        }
     181
     182                        set_transient( 'featured_content_ids', $featured_ids );
     183                }
     184
     185                // Ensure correct format before return.
     186                return array_map( 'absint', $featured_ids );
    194187        }
    195188
Note: See TracChangeset for help on using the changeset viewer.