Skip to:
Content

BuddyPress.org

Changeset 11007


Ignore:
Timestamp:
08/10/2016 11:40:14 AM (4 years ago)
Author:
djpaul
Message:

Core: prevent DB query for WP's main loop on single item pages.

As of WordPress 4.6, it is possible to bypass the main WP_Query entirely.
This saves us one unnecessary database query.

Fixes #7120

Props r-a-y, DJPaul

Location:
trunk/src/bp-core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-catchuri.php

    r10982 r11007  
    10161016}
    10171017add_action( 'wp_head', '_bp_maybe_remove_rel_canonical', 8 );
     1018
     1019/**
     1020 * Stop WordPress performing a DB query for its main loop.
     1021 *
     1022 * As of WordPress 4.6, it is possible to bypass the main WP_Query entirely.
     1023 * This saves us one unnecessary database query! :)
     1024 *
     1025 * @since 2.7.0
     1026 *
     1027 * @param  null     $retval Current return value for filter.
     1028 * @param  WP_Query $query  Current WordPress query object.
     1029 * @return null|array
     1030 */
     1031function bp_core_filter_wp_query( $retval, $query ) {
     1032    if ( ! $query->is_main_query() ) {
     1033        return $retval;
     1034    }
     1035
     1036    /*
     1037     * If not on a BP single page, bail.
     1038     * It's too early to use bp_is_single_item() here.
     1039     */
     1040    if ( ! bp_is_group() || ! bp_is_user() || ! bp_is_single_activity() ) {
     1041        return $retval;
     1042    }
     1043
     1044    $query->found_posts   = 0;
     1045    $query->max_num_pages = 0;
     1046
     1047    // Return something other than a null value to bypass WP_Query.
     1048    return array();
     1049}
  • trunk/src/bp-core/bp-core-filters.php

    r10941 r11007  
    7575// Turn comments off for BuddyPress pages.
    7676add_filter( 'comments_open', 'bp_comments_open', 10, 2 );
     77
     78// Prevent DB query for WP's main loop.
     79add_filter( 'posts_pre_query', 'bp_core_filter_wp_query', 10, 2 );
    7780
    7881/**
Note: See TracChangeset for help on using the changeset viewer.