bp_core_filter_wp_query( null $retval, WP_Query $query )

Stop WordPress performing a DB query for its main loop.


Description Description

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


Top ↑

Parameters Parameters

$retval

(null) (Required) Current return value for filter.

$query

(WP_Query) (Required) Current WordPress query object.


Top ↑

Return Return

(null|array)


Source Source

File: bp-core/bp-core-catchuri.php

function bp_core_filter_wp_query( $retval, $query ) {
	if ( ! $query->is_main_query() ) {
		return $retval;
	}

	/*
	 * If not on a BP single page, bail.
	 * Too early to use bp_is_single_item(), so use BP conditionals.
	 */
	if ( false === ( bp_is_group() || bp_is_user() || bp_is_single_activity() ) ) {
		return $retval;
	}

	// Set default properties as recommended in the 'posts_pre_query' DocBlock.
	$query->found_posts   = 0;
	$query->max_num_pages = 0;

	// Return something other than a null value to bypass WP_Query.
	return array();
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.