Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/02/2022 08:24:07 PM (2 years ago)
Author:
imath
Message:

Blogs: improve the way post featured image are fetched in blogs loop

  1. Make sure to switch blog before using has_post_thumbnail() so that post featured image about sub blogs are fetched.
  2. Introduce the bp_blogs_fetch_latest_post_thumbnails filter to let advanced users skip queries about these post featured images by returning false.

Props boonebgorges

Fixes #8641

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-blogs/classes/class-bp-blogs-blog.php

    r13236 r13307  
    627627            $blog_prefix = $wpdb->get_blog_prefix( $paged_blogs[$i]->blog_id );
    628628            $paged_blogs[$i]->latest_post = $wpdb->get_row( "SELECT ID, post_content, post_title, post_excerpt, guid FROM {$blog_prefix}posts WHERE post_status = 'publish' AND post_type = 'post' AND id != 1 ORDER BY id DESC LIMIT 1" );
    629             $images = array();
    630 
    631             // Add URLs to any Featured Image this post might have.
    632             if ( ! empty( $paged_blogs[$i]->latest_post ) && has_post_thumbnail( $paged_blogs[$i]->latest_post->ID ) ) {
    633 
    634                 // Grab 4 sizes of the image. Thumbnail.
    635                 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'thumbnail', false );
    636                 if ( ! empty( $image ) )
    637                     $images['thumbnail'] = $image[0];
    638 
    639                 // Medium.
    640                 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'medium', false );
    641                 if ( ! empty( $image ) )
    642                     $images['medium'] = $image[0];
    643 
    644                 // Large.
    645                 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'large', false );
    646                 if ( ! empty( $image ) )
    647                     $images['large'] = $image[0];
    648 
    649                 // Post thumbnail.
    650                 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'post-thumbnail', false );
    651                 if ( ! empty( $image ) )
    652                     $images['post-thumbnail'] = $image[0];
    653 
    654                 // Add the images to the latest_post object.
    655                 $paged_blogs[$i]->latest_post->images = $images;
     629
     630            /**
     631             * Filters whether to fetch Featured Images for the Sites directory.
     632             *
     633             * @since 11.0.0
     634             *
     635             * @param bool $fetch_featured_images Defaults to true.
     636             * @param int  $blog_id               ID of the current blog whose extras are being generated.
     637             */
     638            if ( apply_filters( 'bp_blogs_fetch_latest_post_thumbnails', true, $paged_blogs[ $i ]->blog_id ) ) {
     639                $images = array();
     640
     641                switch_to_blog( $paged_blogs[ $i ]->blog_id );
     642
     643                // Add URLs to any Featured Image this post might have.
     644                if ( ! empty( $paged_blogs[$i]->latest_post ) && has_post_thumbnail( $paged_blogs[$i]->latest_post->ID ) ) {
     645
     646                    // Grab 4 sizes of the image. Thumbnail.
     647                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'thumbnail', false );
     648                    if ( ! empty( $image ) )
     649                        $images['thumbnail'] = $image[0];
     650
     651                    // Medium.
     652                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'medium', false );
     653                    if ( ! empty( $image ) )
     654                        $images['medium'] = $image[0];
     655
     656                    // Large.
     657                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'large', false );
     658                    if ( ! empty( $image ) )
     659                        $images['large'] = $image[0];
     660
     661                    // Post thumbnail.
     662                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'post-thumbnail', false );
     663                    if ( ! empty( $image ) )
     664                        $images['post-thumbnail'] = $image[0];
     665
     666                    // Add the images to the latest_post object.
     667                    $paged_blogs[$i]->latest_post->images = $images;
     668                }
     669
     670                restore_current_blog();
    656671            }
    657672        }
Note: See TracChangeset for help on using the changeset viewer.