BuddyDev

Search

Buddyblog limit submissions addon suggestion

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #43635

    Hi Tosin,
    Thank you for reminding. Our priorities have changed and this addon for BuddyBlog is not in our priority list currently.

    We do plan to create it sometime sin the future.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 2
    ndh01 on #51344

    I too would like an option to limit the number of posts a user can create per post type. Is this feature still coming?

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #51346

    Hello,

    Please try the following code:

    
    add_filter( 'bblpro_user_can_create_post', function ( $can, $user_id, $post_type ) {
    
    	// Not his profile return.
    	if ( ! bp_is_my_profile() ) {
    		return $can;
    	}
    
    	if ( 'post' === $post_type ) {
    		$posts_limit = 5;
    
    		$args = array(
    			'numberposts' => - 1,
    			'post_type'   => $post_type,
    			'fields'      => 'ids',
    			'author'      => $user_id,
    		);
    
    		$posts_count = get_posts( $args );
    
    		if ( ! empty( $posts_count ) && count( $posts_count ) >= $posts_limit ) {
    			$can = false;
    		}
    	}
    
    	return $can;
    }, 15, 3 );
    
    

    It will limit the user based on post type. Please give it a try

    Regards
    Ravi

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #51800

    Hello Ravi

    I would prefer to limit post creation to 10 per day for all users

    Maybe a buddyblog integration with this plugin can help https://wordpress.org/plugins/user-posts-limit/

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #51801

    this is a rough work

    
     add_filter( 'bblpro_user_can_create_post', function ( $can, $user_id, $post_type ) {
    
        // Check if it's a post creation attempt and the user is logged in.
        if ( 'post' !== $post_type || ! is_user_logged_in() ) {
            return $can;
        }
    
        // Get the posts limit.
        $posts_limit = 1;
    
        // Get the last post creation timestamp for the user.
        $last_post_time = get_user_meta( $user_id, 'last_post_time', true );
    
        // If the last post time is not set or it's been more than 24 hours, reset the counter.
        if ( empty( $last_post_time ) || strtotime( $last_post_time ) < strtotime( '-1 day' ) ) {
            // Reset post count.
            update_user_meta( $user_id, 'post_count', 0 );
            update_user_meta( $user_id, 'last_post_time', current_time( 'mysql' ) );
        }
    
        // Get the current post count for the user.
        $post_count = get_user_meta( $user_id, 'post_count', true );
    
        if ( $post_count >= $posts_limit ) {
            $can = false; // User has exceeded the daily limit.
        } else {
            // Increment the post count and update the last post time.
            $post_count++;
            update_user_meta( $user_id, 'post_count', $post_count );
            update_user_meta( $user_id, 'last_post_time', current_time( 'mysql' ) );
        }
    
        return $can;
    }, 15, 3 ); 
    

    The problem is how to not restrict old users who have already published old posts over the post limit. I would have preferred the limit to start afresh for old users

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #51802

    Hello Ravi,

    I finally think this code work can you review it

     add_filter( 'bblpro_user_can_create_post', function ( $can, $user_id, $post_type ) {
    
        // Not user's profile, return without modification
        if ( ! bp_is_my_profile() ) {
            return $can;
        }
    
        if ( 'post' === $post_type ) {
            $posts_limit = 5; // Adjust the daily limit as needed
    
            $today = date( 'Y-m-d' ); // Get today's date
    
            $args = array(
                'numberposts' => -1,
                'post_type'   => $post_type,
                'fields'      => 'ids',
                'author'      => $user_id,
                'date_query'  => array(
                    array(
                        'after'     => $today . ' 00:00:00',
                        'before'    => $today . ' 23:59:59',
                        'inclusive' => true,
                    ),
                ),
            );
    
            $posts_count = get_posts( $args );
    
            if ( ! empty( $posts_count ) && count( $posts_count ) >= $posts_limit ) {
                $can = false; // User has reached the daily limit
            }
        }
    
        return $can;
    }, 15, 3 );
     
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #51805

    Hello Tosin,

    You can use this code although it is slow but it doesn’t have much effect on site performance.

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: not resolved