| | 1000 | |
| | 1001 | /** |
| | 1002 | * Do not do a DB query for the main WP_Query on BuddyPress single pages. |
| | 1003 | * |
| | 1004 | * As of WordPress 4.6, it is possible to bypass the main WP_Query entirely. |
| | 1005 | * This saves us one unnecessary database query! :) |
| | 1006 | * |
| | 1007 | * @since 2.7.0 |
| | 1008 | * |
| | 1009 | * @param null $retval Current return value for filter. |
| | 1010 | * @param WP_Query $q Current WordPress query object. |
| | 1011 | * @return null|array |
| | 1012 | */ |
| | 1013 | function bp_posts_pre_query_filter( $retval, $q ) { |
| | 1014 | // Bail if not main query. |
| | 1015 | if ( false === $q->is_main_query() ) { |
| | 1016 | return $retval; |
| | 1017 | } |
| | 1018 | |
| | 1019 | // Too early to check bp_is_single_item(), so check BP conditionals. |
| | 1020 | // If not on a BP single page, bail. |
| | 1021 | if ( false === ( bp_is_group() || bp_is_user() ) ) { |
| | 1022 | return $retval; |
| | 1023 | } |
| | 1024 | |
| | 1025 | // Set default properties as recommended in the 'posts_pre_query' DocBlock. |
| | 1026 | $q->found_posts = 0; |
| | 1027 | $q->max_num_pages = 0; |
| | 1028 | |
| | 1029 | // Return something other than a null value to bypass WP_Query. |
| | 1030 | return array(); |
| | 1031 | } |
| | 1032 | add_filter( 'posts_pre_query', 'bp_posts_pre_query_filter', 10, 2 ); |