Skip to:
Content
Pages
Categories
Search
Top
Bottom

Exclude post categories, Personalize custom post type


  • MatrixMedia
    Participant

    @matrixmedia

    Hello
    I would exclude automatically, some categories of the blog post, in the activity stream. It’s possible?

    I saw that the same problem was seen here: https://buddypress.org/support/topic/possible-to-exclude-post-categories-in-activity-stream-and-make-a-user-profile-private-like-with-pri/

    But applying the same function with the latest version of BuddyPress, it does not work. is possilibe?

    I also tried to customize the Post Types default ‘post’, following this guide: https://codex.buddypress.org/plugindev/post-types-activities/

    changing fields as well:

    add_post_type_support( 'post', 'buddypress-activity' );
    function customize_page_tracking_args() {
        // Check if the Activity component is active before using it.
        if ( ! bp_is_active( 'activity' ) ) {
            return;
        }
        bp_activity_set_post_type_tracking_args( 'post', array(
            'component_id'             => buddypress()->blogs->id,
            'action_id'                => 'new_blog_post', // or 'new_post'
            'bp_activity_admin_filter' => __( 'Published a new post', 'emt-buddypress' ),
            'bp_activity_front_filter' => __( 'Posts', 'emt-buddypress' ),
            'contexts'                 => array( 'activity', 'member' ),
            'activity_comment'         => true,
            'bp_activity_new_post'     => __( '%1$s shared a new <a href="%2$s">post</a>', 'emt-textbuddypress' ),
            'bp_activity_new_post_ms'  => __( '%1$s sahred a new <a href="%2$s">post</a>, on the site %3$s', 'emt-textbuddypress' ),
            'position'                 => 100,
        ) );
    }
    add_action( 'bp_init', 'customize_page_tracking_args' );

    Finally I tried to customize the content of the post, in activity stream, as always suggested in this post:
    guide:https://codex.buddypress.org/plugindev/post-types-activities/
    or this:
    https://buddypress.org/support/topic/custom-activity-new-post-content-not-broken-after-update-the-post/

    This is my function:

    function record_cpt_activity_content( $cpt ) {		
        if ( 'new_post' === $cpt['type'] ) {
            $cpt['content'] = '<a href="' . get_permalink( $post->ID ) . '" class="activity-content-title" >' $post->ID). '</span><br />' 
    		. get_the_post_thumbnail( $post->ID, '') . '<br /> <br />'
    		. get_post_field('post_content', $post->ID);
        }
        return $cpt;	
    }
    add_filter('bp_before_activity_add_parse_args', 'record_cpt_activity_content');

    The problem is that the added HTML

    <a href="' . get_permalink( $post->ID ) . '" class="activity-content-title" >' $post->ID). '</span><br />

    tags are not preserved when the post is published. Solutions?

    Thanks for your attention and assistance

Viewing 5 replies - 1 through 5 (of 5 total)

  • MatrixMedia
    Participant

    @matrixmedia

    sorry, I have not explained well, the second point, I Repeat:

    I also tried to customize the Post Types default ‘post’, following this guide: https://codex.buddypress.org/plugindev/post-types-activities/

    this is my function:

    add_post_type_support( 'post', 'buddypress-activity' );
    function customize_page_tracking_args() {
        // Check if the Activity component is active before using it.
        if ( ! bp_is_active( 'activity' ) ) {
            return;
        }
        bp_activity_set_post_type_tracking_args( 'post', array(
            'component_id'             => buddypress()->blogs->id,
            'action_id'                => 'new_blog_post', // or 'new_post'
            'bp_activity_admin_filter' => __( 'Published a new post', 'emt-buddypress' ),
            'bp_activity_front_filter' => __( 'Posts', 'emt-buddypress' ),
            'contexts'                 => array( 'activity', 'member' ),
            'activity_comment'         => true,
            'bp_activity_new_post'     => __( '%1$s shared a new <a href="%2$s">post</a>', 'emt-textbuddypress' ),
            'bp_activity_new_post_ms'  => __( '%1$s sahred a new <a href="%2$s">post</a>, on the site %3$s', 'emt-textbuddypress' ),
            'position'                 => 100,
        ) );
    }
    add_action( 'bp_init', 'customize_page_tracking_args' );

    but when I publish a post on activity stream, is not displayed. If I remove the function or use a new custom_post_type, in this function, it works.


    MatrixMedia
    Participant

    @matrixmedia

    to exclude some categories I have tried this:

    function exclude_category_slugs_from_activity_stream( $new_status, $old_status, $post ) {
    
    	// Only record published posts
    	if ( 'publish' !== $new_status ) {
    		return;
    	}
    
    	// Don't record edits (publish -> publish)
    	if ( 'publish' === $old_status ) {
    		return;
    	}
    
    	// add in any categories to exclue from activity stream, separated by commas, no spaces
    	$category_slugs_to_exclude = "news,new,News,service,Service";
    
    	// create the array that contains the category ids to exclude
    	$ids_to_exclude = array();
    	// create new array by splitting category slugs by comma
    	$category_slug_array = split( ",", $category_slugs_to_exclude );
    	// iterate over category_slug_array
    	foreach ( $category_slug_array as $category ) {
    		// get the category id based on the slug
    		$idObj = get_category_by_slug( $category );
    		$id = $idObj->term_id;
    		// push the category id onto the exclude array
    		array_push ( $ids_to_exclude, $id );
    	}
    
    	// get the post's categories
    	$categories = get_the_category( $post->ID );
    	$in = false;
    
    	// check if the post has any categories, do nothing if not
    	if( count($categories) > 0 ) {
    		// iterate over categories
    		foreach ( $categories as $category ) {
    			// check if any excluded category exists within post's categories
    			if( in_array( $category->cat_ID, $ids_to_exclude) )
    				$in = true;
    		}
    	}
    
    	// Don't record posts from filtered categories
    	if( $in )
    		return;
    
    	return bp_activity_post_type_publish( $post->ID, $post );
    }
    
    add_action( 'bp_init', 'bp_blogs_catch_filtered_published_post' );
    
    function bp_blogs_catch_filtered_published_post() {
    	if ( bp_is_active( 'blogs' ) ) {
    		remove_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10 );
    		add_action( 'transition_post_status', 'exclude_category_slugs_from_activity_stream', 10, 3 );
    	}
    }

    I found here: https://premium.wpmudev.org/forums/topic/exclude-auto-blog-posts-from-buddypress-activity-stream

    of course I changed the deprecated functions:
    bp_blogs_record_post -> bp_activity_post_type_publish
    bp_blogs_catch_transition_post_status -> bp_activity_catch_transition_post_type_status

    but nothing … not working … HELP!!!


    wzshop
    Participant

    @wzshop

    Did you manage to fix this?


    rmstm
    Participant

    @rmstm

    Hello,

    I know this topic is well aged, but I had to find a way to do the same thing and didn’t find any good answer throughout the web. As this post is referenced in Google, here my way to do it :

    function add_excluded_type( $where_conditions, $r, $select_sql, $from_sql, $join_sql ) {
    	$type_to_exclude = 'type_name';
    
    	$where_conditions['excluded_types'] = ! empty( $where_conditions['excluded_types'] )
    		? preg_replace( '/\)$/', ", '$type_to_exclude'$0", $where_conditions['excluded_types'] )
    		: "a.type NOT IN ('$type_to_exclude')";
    	
    	return $where_conditions;
    }
    
    add_filter( 'bp_activity_get_where_conditions', 'add_excluded_type', 10, 5 );

    Hope this help next coders !


    gabelon
    Participant

    @gabelon

    Hi, where should I add the categories to be excluded?
    For example, if I want to exclude from the Activity stream, posts categorized as “category1” and “category2” do I need to replace like this?:

    function add_excluded_type( $where_conditions, $r, $select_sql, $from_sql, $join_sql ) {
    	$type_to_exclude = 'category1,category2';
    
    	$where_conditions['excluded_types'] = ! empty( $where_conditions['excluded_types'] )
    		? preg_replace( '/\)$/', ", '$type_to_exclude'$0", $where_conditions['excluded_types'] )
    		: "a.type NOT IN ('$type_to_exclude')";
    	
    	return $where_conditions;
    }
    
    add_filter( 'bp_activity_get_where_conditions', 'add_excluded_type', 10, 5 );

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar