Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KaineLabs/cf2538d074a84412b7307c2e55b5a01b to your computer and use it in GitHub Desktop.
Save KaineLabs/cf2538d074a84412b7307c2e55b5a01b to your computer and use it in GitHub Desktop.
BuddyPress - Exclude Posts From The Activity Stream by Category
<?php
// Exclude posts from the activity stream by category
function filter_activity_stream_by_category($where_conditions, $select_sql, $from_sql, $join_sql, $where_sql) {
// Get the term ID for the parent category 'your-category-slug'
$parent_category_id = get_cat_ID('your-category-slug'); // Change 'your-category-slug' to the slug of the category you would like to exclude and all it's subcategories
// Get all child category IDs of 'category-slug'
$child_categories = get_term_children($parent_category_id, 'category');
$child_categories_ids = array_map('intval', $child_categories); // Ensure they are integers
// Include the parent category ID if also required to be excluded
$child_categories_ids[] = $parent_category_id;
// Convert category IDs to a comma-separated string
$categories_string = implode(',', $child_categories_ids);
// Get posts IDs in these categories
$query_args = array(
'posts_per_page' => -1,
'category_in' => $child_categories_ids, // Use category_in to include children
'fields' => 'ids'
);
$posts_in_categories = get_posts($query_args);
// Convert IDs to string format for SQL query
if (!empty($posts_in_categories)) {
$ids_to_exclude = implode(',', $posts_in_categories);
$where_conditions['exclude_posts'] = "a.secondary_item_id NOT IN ($ids_to_exclude)";
}
return $where_conditions;
}
add_filter('bp_activity_get_where_conditions', 'filter_activity_stream_by_category', 10, 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment