BuddyDev

Search

[Resolved] Activity Sitewide stream filter posts by category

  • Participant
    Level: Initiated
    Posts: 13
    Plamen Peev on #22315

    Hi,

    Anyone know a way to filter new blog posts and comments updates in the activity feed based on post category?

    Thanks a lot

    Plamen

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #22319

    Hi Plamen,
    Thank you for the question.

    At the moment it is not doable in BudyPress out of the box(efficiently as someone might first fetch post ids and then show a working proof).
    If you need to do it, you will need to record the term id along with the activity. In other words, you will need custom code for keeping a sync between the post categories(on new post/update post) and then register new activity actions for each category.

    This is doable but you will most probably need assistance from a developer to accomplish it.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 13
    Plamen Peev on #22321

    Thanks, Brajesh,

    Appreciate your assistance.
    Actually I had a code in place that seemed to work but suddenly stopped.

    Here it is:

    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( 'community', $post_id ) ) {
    			$args->hide_sitewide = false;
    			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 = true;
    			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( 'community', $comment->comment_post_ID ) ) {
    			$args->hide_sitewide = false;
    		} else {
    			$args->hide_sitewide = true;
    		}
    	}
    } }
    add_action( 'bp_activity_before_save', 'ucc_bp_activity_before_save', 10, 1 );
    add_filter( 'bp_after_has_activities_parse_args', 'ray_bp_display_comments_stream' );
    

    Thanks a lot again

    Regards,

    Plamen

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #22322

    Hi Plamen,
    Thank you for sharing.

    If it was working earlier, I can certainly help you making it work again. I am leaving my desk for next 2-3 hours. Will be replying after that.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 13
    Plamen Peev on #22323

    Thanks a lot, Brajesh,

    Best,

    Plamen

  • Participant
    Level: Initiated
    Posts: 13
    Plamen Peev on #22324

    Just a quick update:

    The code below allows me to successfully filter out new posts updates from category ID=1 but it’s not working for new comments.

    function buddydev_disable_category_post_from_activity_recording( $enabled, $blog_id, $post_id, $user_id, $comment_id ) {
    
    	$category_id = 1;
    	$taxonomy    = 'category';
    
    	if ( has_term( $category_id, $taxonomy, $post_id, $comment_id ) ) {  // I could use has_category() but has_terms can be a better example for future customization.
    		$enabled = false;
    	}
    
    	return $enabled;
    }
    
    add_filter( 'bp_activity_post_pre_publish', 'buddydev_disable_category_post_from_activity_recording', 10, 4 );
  • Participant
    Level: Initiated
    Posts: 13
    Plamen Peev on #22362

    Brajesh,

    Any idea how I could tweak the code above to exclude activity updates on new post comments as well?

    Thanks a lot,

    Plamen

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #22363

    Hi Pamen,
    I am sorry for the delayed reply.

    After reding through the codes, I am assuming our goal here to to stop recording comments in certain cases?
    If that is the case, BuddyPress provides us with another filter

    
    bp_activity_{$post_type}_pre_comment
    

    where for normal posts the $post_type is ‘post’.
    So we can attach to ‘bp_activity_post_pre_comment’.

    Here is an example code

    
    function buddydev_disable_activity_blog_comment_recording_conditionally( $enabled, $blog_id, $post_id, $user_id, $comment_id ) {
    
    	// use $post_id to decide.
    	// set $enabled= false; to stop recording comment.
    
    	return $enabled;
    }
    
    add_filter( 'bp_activity_post_pre_comment', 'buddydev_disable_activity_blog_comment_recording_conditionally', 20, 5 );
    

    You may adapt it from there.

    Hope it helps.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 13
    Plamen Peev on #22367

    Thank you so much, Brajesh. Looking up to your skills 🙂 That’s awesome!

    Could I use it with $category_id instead of $post_id or it would not work?
    Maybe I should create a custom post type instead.

    Thanks again – will test it out and report.

    Best,

    Plamen

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #22368

    Hi Plamen,
    Thank you for the kind words.

    The above code is a sample and you can modify it. For example, I am making a change to allow recording comment from ‘community’ category.

    
    
    function buddydev_disable_activity_blog_comment_recording_conditionally( $enabled, $blog_id, $post_id, $user_id, $comment_id ) {
    
    	// use $post_id to decide.
    	// set $enabled= false; to stop recording comment.
    	if ( ! in_category( 'community', $post_id ) ) {
    		$enabled = false;
    	}
    
    	return $enabled;
    }
    
    add_filter( 'bp_activity_post_pre_comment', 'buddydev_disable_activity_blog_comment_recording_conditionally', 20, 5 );
    
    

    If you can tell me which categories to allow or disallow, I can post the updated code too.

    Regards
    Brajesh

The topic ‘ [Resolved] Activity Sitewide stream filter posts by category’ is closed to new replies.

This topic is: resolved