Skip to:
Content

BuddyPress.org

Opened 12 months ago

Last modified 12 months ago

#8343 new defect (bug)

Custom Post Type activity in groups create duplicates

Reported by: etatus Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version: 6.1.0
Component: Activity Keywords:
Cc:

Description

If I add support for "buddypress-activity" to my CPT (with add_post_type_support() and bp_activity_set_post_type_tracking_args() functions) everything works fine.

If I especify 'component_id => 'groups' in bp_activity_set_post_type_tracking_args(), the tracking still works, but item_id is by default the blog ID instead the group ID, so the activity is created but it will not be shown in group activity page.

If I fix this, using bp_after_activity_add_parse_args filter to set item_id to the group ID ($args['item_id'] = bp_get_current_group_id();) the activity is created and it is shown in group activity page.

BUT...

The problem is everytime I update the post, a new activity is generated, so there will be several activities with exactly the same information for one single goup post.

Change History (2)

#1 @etatus
12 months ago

  • Component changed from Core to Activity

#2 @etatus
12 months ago

I found a workaround. It is not elegant, but it seems to avoid the creation of duplicate activities for the same cpt post on updating.

function me_activity_parse_args_filter( $args = array() ) {     
    if ( !empty( $args['type'] ) && $args['type'] == 'new_your-cpt' ) {
                $activities = bp_activity_get(array(
                        'max' => 1,
                        'filter' => array(
                        'action' => 'new_your-cpt',
                        'secondary_id' =>  $args['secondary_item_id'],
                        ),
                ) );
                if (count($activities['activities'])==0) { // The cpt post doesn't have activity yet, so we set the group information
                        $args['component'] = 'groups';
                        if ( !empty(bp_get_current_group_id()) ) $args['item_id'] = bp_get_current_group_id();
                        if (empty($args['content'])) $args['content'] = ' ';  // To make sure content filter applies afterwards 
                } else { // Activity already exists
                        $args['type'] = false; // Delete duplicate activity
                }
    }     
    return $args;
}
add_filter('bp_after_activity_add_parse_args', 'me_activity_parse_args_filter',10,1);
Note: See TracTickets for help on using tickets.