Make WordPress Core

Ticket #25330: 25330.2.patch

File 25330.2.patch, 3.5 KB (added by anonymized_8769252, 12 years ago)
  • wp-content/themes/twentyfourteen/functions.php

     
    408408}
    409409
    410410/**
    411  * Filter the home page posts, and remove formatted posts visible in the sidebar from it
     411 * Remove the posts visible in the sidebar from the blog home.
    412412 *
     413 * @param  WP_Query $query
     414 * @return  void
    413415 */
    414416function twentyfourteen_pre_get_posts( $query ) {
    415         // Bail if not home, not a query, not main query.
    416         if ( ! $query->is_main_query() || is_admin() )
     417        // Bail if we're not on the blog home, if it is not the main query or if we are in the admin.
     418        if ( ! $query->is_home() || ! $query->is_main_query() || is_admin() )
    417419                return;
    418420
    419         // Only on the home page
    420         if ( $query->is_home() ) {
    421                 $exclude_ids = array();
     421        // Use the cached post IDs to exclude if they exist.
     422        $cached_exclude_ids = get_transient( 'twentyfourteen_ephemera_to_exclude' );
    422423
    423                 $videos = twentyfourteen_get_recent( 'post-format-video' );
    424                 $images = twentyfourteen_get_recent( 'post-format-image' );
    425                 $galleries = twentyfourteen_get_recent( 'post-format-gallery' );
    426                 $asides = twentyfourteen_get_recent( 'post-format-aside' );
    427                 $links = twentyfourteen_get_recent( 'post-format-link' );
    428                 $quotes = twentyfourteen_get_recent( 'post-format-quote' );
     424        if ( false !== $cached_exclude_ids ) {
     425                $query->set( 'post__not_in', $cached_exclude_ids );
     426                return;
     427        }
    429428
    430                 foreach ( $videos->posts as $post )
    431                         $exclude_ids[] = $post->ID;
     429        // Fetch the post IDs to exclude and cache them before use.
     430        $widget_settings = get_option( 'widget_widget_twentyfourteen_ephemera' );
    432431
    433                 foreach ( $images->posts as $post )
    434                         $exclude_ids[] = $post->ID;
     432        if ( false === $widget_settings )
     433                return;
    435434
    436                 foreach ( $galleries->posts as $post )
    437                         $exclude_ids[] = $post->ID;
     435        $exclude_ids = array();
    438436
    439                 foreach ( $asides->posts as $post )
    440                         $exclude_ids[] = $post->ID;
     437        foreach ( $widget_settings as $setting ) {
     438                if ( isset( $setting['format'] ) && isset( $setting['number'] ) ) {
     439                        $posts_to_exclude = get_posts( array(
     440                                'order'       => 'DESC',
     441                                'numberposts' => $setting['number'],
     442                                'post_status' => 'publish',
     443                                'tax_query'   => array(
     444                                        array(
     445                                                'taxonomy' => 'post_format',
     446                                                'terms'    => array( 'post-format-' . $setting['format'] ),
     447                                                'field'    => 'slug',
     448                                                'operator' => 'IN',
     449                                        ),
     450                                ),
     451                        ) );
    441452
    442                 foreach ( $links->posts as $post )
    443                         $exclude_ids[] = $post->ID;
     453                        if ( ! empty ( $posts_to_exclude ) ) {
     454                                $posts_to_exclude = wp_list_pluck( $posts_to_exclude, 'ID' );
     455                                $exclude_ids = array_merge( $exclude_ids, $posts_to_exclude );
     456                        }
     457                }
     458        }
    444459
    445                 foreach ( $quotes->posts as $post )
    446                         $exclude_ids[] = $post->ID;
    447 
    448                 $query->set( 'post__not_in', $exclude_ids );
    449         }
     460        $exclude_ids = array_unique( $exclude_ids );
     461        set_transient( 'twentyfourteen_ephemera_to_exclude', $exclude_ids );
     462        $query->set( 'post__not_in', $exclude_ids );
    450463}
    451464add_action( 'pre_get_posts', 'twentyfourteen_pre_get_posts' );
    452465
  • wp-content/themes/twentyfourteen/inc/widgets.php

     
    218218         */
    219219        function flush_widget_cache() {
    220220                delete_transient( $this->id );
     221                delete_transient( 'twentyfourteen_ephemera_to_exclude' );
    221222        }
    222223
    223224        /**