Make WordPress Core

Ticket #25330: 25330.patch

File 25330.patch, 3.5 KB (added by anonymized_8769252, 13 years ago)

This is a first stab at a patch. I'm not sure if this is the most clever way of doing it, so any feedback is appreciated.

  • wp-content/themes/twentyfourteen/functions.php

     
    369369}
    370370
    371371/**
    372  * Filter the home page posts, and remove formatted posts visible in the sidebar from it
     372 * Remove the posts visible in the sidebar from the blog home.
    373373 *
    374374 */
    375375function twentyfourteen_pre_get_posts( $query ) {
    376         // Bail if not home, not a query, not main query.
    377         if ( ! $query->is_main_query() || is_admin() )
     376        // Bail if we're not on the blog home, if it is not the main query or if we are in the admin.
     377        if ( ! $query->is_home() || ! $query->is_main_query() || is_admin() )
    378378                return;
    379379
    380         // Only on the home page
    381         if ( $query->is_home() ) {
    382                 $exclude_ids = array();
     380        // Use the cached post IDs to exclude if they exist.
     381        $cached_exclude_ids = get_transient( 'twentyfourteen_ephemera_to_exclude' );
    383382
    384                 $videos = twentyfourteen_get_recent( 'post-format-video' );
    385                 $images = twentyfourteen_get_recent( 'post-format-image' );
    386                 $galleries = twentyfourteen_get_recent( 'post-format-gallery' );
    387                 $asides = twentyfourteen_get_recent( 'post-format-aside' );
    388                 $links = twentyfourteen_get_recent( 'post-format-link' );
    389                 $quotes = twentyfourteen_get_recent( 'post-format-quote' );
     383        if ( false !== $cached_exclude_ids ) {
     384                $query->set( 'post__not_in', $cached_exclude_ids );
     385                return;
     386        }
    390387
    391                 foreach ( $videos->posts as $post )
    392                         $exclude_ids[] = $post->ID;
     388        // Fetch the post IDs to exclude and cache them before use.
     389        $widget_settings = get_option( 'widget_widget_twentyfourteen_ephemera' );
    393390
    394                 foreach ( $images->posts as $post )
    395                         $exclude_ids[] = $post->ID;
     391        if ( false === $widget_settings )
     392                return;
    396393
    397                 foreach ( $galleries->posts as $post )
    398                         $exclude_ids[] = $post->ID;
     394        $exclude_ids = array();
    399395
    400                 foreach ( $asides->posts as $post )
    401                         $exclude_ids[] = $post->ID;
     396        foreach ( $widget_settings as $setting ) {
     397                if ( isset( $setting['format'] ) &&  isset( $setting['number'] ) ) {
     398                        $posts_to_exclude = new WP_Query( array(
     399                                'order'          => 'DESC',
     400                                'posts_per_page' => $setting['number'],
     401                                'no_found_rows'  => true,
     402                                'post_status'    => 'publish',
     403                                'post__not_in'   => get_option( 'sticky_posts' ),
     404                                'tax_query'      => array(
     405                                        array(
     406                                                'taxonomy' => 'post_format',
     407                                                'terms'    => array( 'post-format-' . $setting['format'] ),
     408                                                'field'    => 'slug',
     409                                                'operator' => 'IN',
     410                                        ),
     411                                ),
     412                                'fields'        => 'ids'
     413                        ) );
    402414
    403                 foreach ( $links->posts as $post )
    404                         $exclude_ids[] = $post->ID;
     415                        if( isset( $posts_to_exclude->posts ) && ! empty ( $posts_to_exclude->posts ) ) {
     416                                $exclude_ids = array_merge( $exclude_ids, $posts_to_exclude->posts );
     417                        }
     418                }
     419        }
    405420
    406                 foreach ( $quotes->posts as $post )
    407                         $exclude_ids[] = $post->ID;
    408 
    409                 $query->set( 'post__not_in', $exclude_ids );
    410         }
     421        set_transient( 'twentyfourteen_ephemera_to_exclude', $exclude_ids );
     422        $query->set( 'post__not_in', $exclude_ids );
    411423}
    412424add_action( 'pre_get_posts', 'twentyfourteen_pre_get_posts' );
    413425
  • 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        /**