Activity Stream and date_query
If you have a lot of activity entries on your site, load times can suffer dramatically when viewing activity streams.
You can use the date_query argument to speed things up by limiting the entries to only those from the past day or week or month, etc.
But you cannot add it as a string in the usual fashion. For example, adding a per_page argument:
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . '&per_page=20' ) ) : ?>
Instead, you need to use an array, which is easy enough until you try to include the value of bp_ajax_querystring( ‘activity’ ) as an argument and the ‘Load More’ button stops working properly.
Here’s the solution. You can paste it in your overload of the activity-loop template or you could use the bp_before_has_activities_parse_args filter.
<?php
$qs = bp_ajax_querystring( 'activity' );
wp_parse_str( $qs, $activity_args );
// set the limit to 1 week ago
$date_limit = date('Y-m-d H:i:s', strtotime('-1 week'));
$activity_args['date_query'] = array(
array(
'after' => $date_limit,
),
);
$activity_args['per_page'] = 5;
?>
<?php if ( bp_has_activities( $activity_args ) ) : ?>
// etc.
Note the use of wp_parse_str to add the query string needed for the ‘Load More’ button.
And you can use conditionals to change the date_query argument depending on which activity stream you’re viewing:
if ( bp_is_activity_directory() ) {
$date_limit = date("Y-m-d H:i:s", strtotime("-1 week"));
} elseif( bp_is_group() ) {
$date_limit = date("Y-m-d H:i:s", strtotime("-1 month"));
} else { // profile stream
$date_limit = date("Y-m-d H:i:s", strtotime("-1 year"));
}