BP_Activity_Activity::get( array $args = array() )
Get activity items, as specified by parameters.
Description Description
See also See also
- BP_Activity_Activity::get_filter_sql(): for a description of the ‘filter’ parameter.
- WP_Meta_Query::queries: for a description of the ‘meta_query’ parameter format.
Parameters Parameters
- $args
-
(Optional) An array of arguments. All items are optional.
- 'page'
(int) Which page of results to fetch. Using page=1 without per_page will result in no pagination. Default: 1. - 'per_page'
(int|bool) Number of results per page. Default: 25. - 'max'
(int|bool) Maximum number of results to return. Default: false (unlimited). - 'fields'
(string) Activity fields to return. Pass 'ids' to get only the activity IDs. 'all' returns full activity objects. - 'sort'
(string) ASC or DESC. Default: 'DESC'. - 'order_by'
(string) Column to order results by. - 'exclude'
(array) Array of activity IDs to exclude. Default: false. - 'in'
(array) Array of ids to limit query by (IN). Default: false. - 'meta_query'
(array) Array of meta_query conditions. See WP_Meta_Query::queries. - 'date_query'
(array) Array of date_query conditions. See first parameter of WP_Date_Query::__construct(). - 'filter_query'
(array) Array of advanced query conditions. See BP_Activity_Query::__construct(). - 'scope'
(string|array) Pre-determined set of activity arguments. - 'filter'
(array) See BP_Activity_Activity::get_filter_sql(). - 'search_terms'
(string) Limit results by a search term. Default: false. - 'display_comments'
(bool) Whether to include activity comments. Default: false. - 'show_hidden'
(bool) Whether to show items marked hide_sitewide. Default: false. - 'spam'
(string) Spam status. Default: 'ham_only'. - 'update_meta_cache'
(bool) Whether to pre-fetch metadata for queried activity items. Default: true. - 'count_total'
(string|bool) If true, an additional DB query is run to count the total activity items for the query. Default: false.
Default value: array()
- 'page'
Return Return
(array) The array returned has two keys: - 'total' is the count of located activities - 'activities' is an array of the located activities
Source Source
File: bp-activity/classes/class-bp-activity-activity.php
public static function get( $args = array() ) {
global $wpdb;
$function_args = func_get_args();
// Backward compatibility with old method of passing arguments.
if ( !is_array( $args ) || count( $function_args ) > 1 ) {
_deprecated_argument( __METHOD__, '1.6', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
$old_args_keys = array(
0 => 'max',
1 => 'page',
2 => 'per_page',
3 => 'sort',
4 => 'search_terms',
5 => 'filter',
6 => 'display_comments',
7 => 'show_hidden',
8 => 'exclude',
9 => 'in',
10 => 'spam'
);
$args = bp_core_parse_args_array( $old_args_keys, $function_args );
}
$bp = buddypress();
$r = wp_parse_args( $args, array(
'page' => 1, // The current page.
'per_page' => 25, // Activity items per page.
'max' => false, // Max number of items to return.
'fields' => 'all', // Fields to include.
'sort' => 'DESC', // ASC or DESC.
'order_by' => 'date_recorded', // Column to order by.
'exclude' => false, // Array of ids to exclude.
'in' => false, // Array of ids to limit query by (IN).
'meta_query' => false, // Filter by activitymeta.
'date_query' => false, // Filter by date.
'filter_query' => false, // Advanced filtering - see BP_Activity_Query.
'filter' => false, // See self::get_filter_sql().
'scope' => false, // Preset activity arguments.
'search_terms' => false, // Terms to search by.
'display_comments' => false, // Whether to include activity comments.
'show_hidden' => false, // Show items marked hide_sitewide.
'spam' => 'ham_only', // Spam status.
'update_meta_cache' => true, // Whether or not to update meta cache.
'count_total' => false, // Whether or not to use count_total.
) );
// Select conditions.
$select_sql = "SELECT DISTINCT a.id";
$from_sql = " FROM {$bp->activity->table_name} a";
$join_sql = '';
// Where conditions.
$where_conditions = array();
// Excluded types.
$excluded_types = array();
// Scope takes precedence.
if ( ! empty( $r['scope'] ) ) {
$scope_query = self::get_scope_query_sql( $r['scope'], $r );
// Add our SQL conditions if matches were found.
if ( ! empty( $scope_query['sql'] ) ) {
$where_conditions['scope_query_sql'] = $scope_query['sql'];
}
// Override some arguments if needed.
if ( ! empty( $scope_query['override'] ) ) {
$r = array_replace_recursive( $r, $scope_query['override'] );
}
// Advanced filtering.
} elseif ( ! empty( $r['filter_query'] ) ) {
$filter_query = new BP_Activity_Query( $r['filter_query'] );
$sql = $filter_query->get_sql();
if ( ! empty( $sql ) ) {
$where_conditions['filter_query_sql'] = $sql;
}
}
// Regular filtering.
if ( $r['filter'] && $filter_sql = BP_Activity_Activity::get_filter_sql( $r['filter'] ) ) {
$where_conditions['filter_sql'] = $filter_sql;
}
// Spam.
if ( 'ham_only' == $r['spam'] ) {
$where_conditions['spam_sql'] = 'a.is_spam = 0';
} elseif ( 'spam_only' == $r['spam'] ) {
$where_conditions['spam_sql'] = 'a.is_spam = 1';
}
// Searching.
if ( $r['search_terms'] ) {
$search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%';
$where_conditions['search_sql'] = $wpdb->prepare( 'a.content LIKE %s', $search_terms_like );
/**
* Filters whether or not to include users for search parameters.
*
* @since 3.0.0
*
* @param bool $value Whether or not to include user search. Default false.
*/
if ( apply_filters( 'bp_activity_get_include_user_search', false ) ) {
$user_search = get_user_by( 'slug', $r['search_terms'] );
if ( false !== $user_search ) {
$user_id = $user_search->ID;
$where_conditions['search_sql'] .= $wpdb->prepare( ' OR a.user_id = %d', $user_id );
}
}
}
// Sorting.
$sort = $r['sort'];
if ( $sort != 'ASC' && $sort != 'DESC' ) {
$sort = 'DESC';
}
switch( $r['order_by'] ) {
case 'id' :
case 'user_id' :
case 'component' :
case 'type' :
case 'action' :
case 'content' :
case 'primary_link' :
case 'item_id' :
case 'secondary_item_id' :
case 'date_recorded' :
case 'hide_sitewide' :
case 'mptt_left' :
case 'mptt_right' :
case 'is_spam' :
break;
default :
$r['order_by'] = 'date_recorded';
break;
}
$order_by = 'a.' . $r['order_by'];
// Hide Hidden Items?
if ( ! $r['show_hidden'] ) {
$where_conditions['hidden_sql'] = "a.hide_sitewide = 0";
}
// Exclude specified items.
if ( ! empty( $r['exclude'] ) ) {
$exclude = implode( ',', wp_parse_id_list( $r['exclude'] ) );
$where_conditions['exclude'] = "a.id NOT IN ({$exclude})";
}
// The specific ids to which you want to limit the query.
if ( ! empty( $r['in'] ) ) {
$in = implode( ',', wp_parse_id_list( $r['in'] ) );
$where_conditions['in'] = "a.id IN ({$in})";
}
// Process meta_query into SQL.
$meta_query_sql = self::get_meta_query_sql( $r['meta_query'] );
if ( ! empty( $meta_query_sql['join'] ) ) {
$join_sql .= $meta_query_sql['join'];
}
if ( ! empty( $meta_query_sql['where'] ) ) {
$where_conditions[] = $meta_query_sql['where'];
}
// Process date_query into SQL.
$date_query_sql = self::get_date_query_sql( $r['date_query'] );
if ( ! empty( $date_query_sql ) ) {
$where_conditions['date'] = $date_query_sql;
}
// Alter the query based on whether we want to show activity item
// comments in the stream like normal comments or threaded below
// the activity.
if ( false === $r['display_comments'] || 'threaded' === $r['display_comments'] ) {
$excluded_types[] = 'activity_comment';
}
// Exclude 'last_activity' items unless the 'action' filter has
// been explicitly set.
if ( empty( $r['filter']['object'] ) ) {
$excluded_types[] = 'last_activity';
}
// Build the excluded type sql part.
if ( ! empty( $excluded_types ) ) {
$not_in = "'" . implode( "', '", esc_sql( $excluded_types ) ) . "'";
$where_conditions['excluded_types'] = "a.type NOT IN ({$not_in})";
}
/**
* Filters the MySQL WHERE conditions for the Activity items get method.
*
* @since 1.9.0
*
* @param array $where_conditions Current conditions for MySQL WHERE statement.
* @param array $r Parsed arguments passed into method.
* @param string $select_sql Current SELECT MySQL statement at point of execution.
* @param string $from_sql Current FROM MySQL statement at point of execution.
* @param string $join_sql Current INNER JOIN MySQL statement at point of execution.
*/
$where_conditions = apply_filters( 'bp_activity_get_where_conditions', $where_conditions, $r, $select_sql, $from_sql, $join_sql );
// Join the where conditions together.
$where_sql = 'WHERE ' . join( ' AND ', $where_conditions );
/**
* Filter the MySQL JOIN clause for the main activity query.
*
* @since 2.5.0
*
* @param string $join_sql JOIN clause.
* @param array $r Method parameters.
* @param string $select_sql Current SELECT MySQL statement.
* @param string $from_sql Current FROM MySQL statement.
* @param string $where_sql Current WHERE MySQL statement.
*/
$join_sql = apply_filters( 'bp_activity_get_join_sql', $join_sql, $r, $select_sql, $from_sql, $where_sql );
// Sanitize page and per_page parameters.
$page = absint( $r['page'] );
$per_page = absint( $r['per_page'] );
$retval = array(
'activities' => null,
'total' => null,
'has_more_items' => null,
);
/**
* Filters if BuddyPress should use legacy query structure over current structure for version 2.0+.
*
* It is not recommended to use the legacy structure, but allowed to if needed.
*
* @since 2.0.0
*
* @param bool $value Whether to use legacy structure or not.
* @param BP_Activity_Activity $value Current method being called.
* @param array $r Parsed arguments passed into method.
*/
if ( apply_filters( 'bp_use_legacy_activity_query', false, __METHOD__, $r ) ) {
// Legacy queries joined against the user table.
$select_sql = "SELECT DISTINCT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name";
$from_sql = " FROM {$bp->activity->table_name} a LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID";
if ( ! empty( $page ) && ! empty( $per_page ) ) {
$pag_sql = $wpdb->prepare( "LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page );
/** This filter is documented in bp-activity/bp-activity-classes.php */
$activity_sql = apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, a.id {$sort} {$pag_sql}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql );
} else {
$pag_sql = '';
/**
* Filters the legacy MySQL query statement so plugins can alter before results are fetched.
*
* @since 1.5.0
*
* @param string $value Concatenated MySQL statement pieces to be query results with for legacy query.
* @param string $select_sql Final SELECT MySQL statement portion for legacy query.
* @param string $from_sql Final FROM MySQL statement portion for legacy query.
* @param string $where_sql Final WHERE MySQL statement portion for legacy query.
* @param string $sort Final sort direction for legacy query.
*/
$activity_sql = apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, a.id {$sort}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql );
}
/*
* Queries that include 'last_activity' are cached separately,
* since they are generally much less long-lived.
*/
if ( preg_match( '/a\.type NOT IN \([^\)]*\'last_activity\'[^\)]*\)/', $activity_sql ) ) {
$cache_group = 'bp_activity';
} else {
$cache_group = 'bp_activity_with_last_activity';
}
$activities = $wpdb->get_results( $activity_sql );
// Integer casting for legacy activity query.
foreach ( (array) $activities as $i => $ac ) {
$activities[ $i ]->id = (int) $ac->id;
$activities[ $i ]->item_id = (int) $ac->item_id;
$activities[ $i ]->secondary_item_id = (int) $ac->secondary_item_id;
$activities[ $i ]->user_id = (int) $ac->user_id;
$activities[ $i ]->hide_sitewide = (int) $ac->hide_sitewide;
$activities[ $i ]->mptt_left = (int) $ac->mptt_left;
$activities[ $i ]->mptt_right = (int) $ac->mptt_right;
$activities[ $i ]->is_spam = (int) $ac->is_spam;
}
} else {
// Query first for activity IDs.
$activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY {$order_by} {$sort}, a.id {$sort}";
if ( ! empty( $per_page ) && ! empty( $page ) ) {
// We query for $per_page + 1 items in order to
// populate the has_more_items flag.
$activity_ids_sql .= $wpdb->prepare( " LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page + 1 );
}
/**
* Filters the paged activities MySQL statement.
*
* @since 2.0.0
*
* @param string $activity_ids_sql MySQL statement used to query for Activity IDs.
* @param array $r Array of arguments passed into method.
*/
$activity_ids_sql = apply_filters( 'bp_activity_paged_activities_sql', $activity_ids_sql, $r );
/*
* Queries that include 'last_activity' are cached separately,
* since they are generally much less long-lived.
*/
if ( preg_match( '/a\.type NOT IN \([^\)]*\'last_activity\'[^\)]*\)/', $activity_ids_sql ) ) {
$cache_group = 'bp_activity';
} else {
$cache_group = 'bp_activity_with_last_activity';
}
$cached = bp_core_get_incremented_cache( $activity_ids_sql, $cache_group );
if ( false === $cached ) {
$activity_ids = $wpdb->get_col( $activity_ids_sql );
bp_core_set_incremented_cache( $activity_ids_sql, $cache_group, $activity_ids );
} else {
$activity_ids = $cached;
}
$retval['has_more_items'] = ! empty( $per_page ) && count( $activity_ids ) > $per_page;
// If we've fetched more than the $per_page value, we
// can discard the extra now.
if ( ! empty( $per_page ) && count( $activity_ids ) === $per_page + 1 ) {
array_pop( $activity_ids );
}
if ( 'ids' === $r['fields'] ) {
$activities = array_map( 'intval', $activity_ids );
} else {
$activities = self::get_activity_data( $activity_ids );
}
}
if ( 'ids' !== $r['fields'] ) {
// Get the fullnames of users so we don't have to query in the loop.
$activities = self::append_user_fullnames( $activities );
// Get activity meta.
$activity_ids = array();
foreach ( (array) $activities as $activity ) {
$activity_ids[] = $activity->id;
}
if ( ! empty( $activity_ids ) && $r['update_meta_cache'] ) {
bp_activity_update_meta_cache( $activity_ids );
}
if ( $activities && $r['display_comments'] ) {
$activities = BP_Activity_Activity::append_comments( $activities, $r['spam'] );
}
// Pre-fetch data associated with activity users and other objects.
BP_Activity_Activity::prefetch_object_data( $activities );
// Generate action strings.
$activities = BP_Activity_Activity::generate_action_strings( $activities );
}
$retval['activities'] = $activities;
// If $max is set, only return up to the max results.
if ( ! empty( $r['count_total'] ) ) {
/**
* Filters the total activities MySQL statement.
*
* @since 1.5.0
*
* @param string $value MySQL statement used to query for total activities.
* @param string $where_sql MySQL WHERE statement portion.
* @param string $sort Sort direction for query.
*/
$total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', "SELECT count(DISTINCT a.id) FROM {$bp->activity->table_name} a {$join_sql} {$where_sql}", $where_sql, $sort );
$cached = bp_core_get_incremented_cache( $total_activities_sql, $cache_group );
if ( false === $cached ) {
$total_activities = $wpdb->get_var( $total_activities_sql );
bp_core_set_incremented_cache( $total_activities_sql, $cache_group, $total_activities );
} else {
$total_activities = $cached;
}
if ( !empty( $r['max'] ) ) {
if ( (int) $total_activities > (int) $r['max'] ) {
$total_activities = $r['max'];
}
}
$retval['total'] = $total_activities;
}
return $retval;
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced the $order_by parameter. |
| 2.4.0 | Introduced the $fields parameter. |
| 1.2.0 | Introduced. |