Skip to content

Instantly share code, notes, and snippets.

@jmdodd
Created December 16, 2011 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmdodd/1486717 to your computer and use it in GitHub Desktop.
Save jmdodd/1486717 to your computer and use it in GitHub Desktop.
Remove posts with category (or categories) from BuddyPress Activity Stream
<?php
// Move the bp_blogs_record_post action to the later wp_insert_post, so that in_category will return the correct result.
if ( ! function_exists( 'ucc_wp_insert_post_bp_blogs_record_post' ) ) {
function ucc_wp_insert_post_bp_blogs_record_post() {
remove_action( 'save_post', 'bp_blogs_record_post', 10, 2 );
add_action( 'wp_insert_post', 'bp_blogs_record_post', 10, 2 );
} }
add_action( 'init', 'ucc_wp_insert_post_bp_blogs_record_post' );
// Set hide_sitewide to true if the activity should be excluded.
if ( ! function_exists( 'ucc_bp_activity_before_save' ) ) {
function ucc_bp_activity_before_save( &$args ) {
global $wpdb;
if ('new_blog_post' == $args->type ) {
$post_id = $args->secondary_item_id;
$comment_ids = array();
$comments = get_comments( "post_id=$post_id" );
foreach ( $comments as $comment ) {
$comment_ids[] = $comment->comment_ID;
}
if ( in_category( 'exclude-me', $post_id ) ) {
$args->hide_sitewide = true;
if ( ! empty( $comment_ids ) ) {
$result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->prefix" . "bp_activity SET hide_sitewide = 1 WHERE secondary_item_id IN (" . implode( ',', $comment_ids ) . ")" ) );
}
} else {
$args->hide_sitewide = false;
if ( ! empty( $comment_ids ) ) {
$result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->prefix" . "bp_activity SET hide_sitewide = 0 WHERE secondary_item_id IN (" . implode( ',', $comment_ids ) . ")" ) );
}
}
}
if ( 'new_blog_comment' == $args->type ) {
$comment_id = $args->secondary_item_id;
$comment = get_comment( $comment_id );
if ( in_category( 'exclude-me', $comment->comment_post_ID ) ) {
$args->hide_sitewide = true;
} else {
$args->hide_sitewide = false;
}
}
} }
add_action( 'bp_activity_before_save', 'ucc_bp_activity_before_save', 10, 1 );
// Of note: the following function seemed to work, but only satisfied the remove_action for new posts. There were also
// issues with users updating "safe" posts to be in a category that was supposed to be excluded, making it a solution that
// worked about half of the time, and thus, not a solution at all.
//
// if ( ! function_exists( 'ucc_bp_blogs_record_post' ) ) {
// function ucc_bp_blogs_record_post( $post_id, $post, $user_id = false ) {
// // One or more categories specified by ID (integer), name or slug (string), or an array of these
// $categories = 'exclude-me';
//
// if ( in_category( $categories, $post_id ) )
// remove_action( 'save_post', 'bp_blogs_record_post' );
// } }
// add_action( 'save_post', 'ucc_bp_blogs_record_post', 9, 3 );
/*
Copyright 2012 Jennifer M. Dodd (email: jmdodd@gmail.com)
Released under the GPLv2 (or later).
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment