BuddyDev

Search

[Resolved] Auto feature members

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45923

    Hello,

    How can I automatically mark users as featured when their post count equals 25 published posts.

    Thanks

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

    Hi Tosin,
    Thank you for the question.

    The plugin does not support automatic featuring. It does have an api for marking user as featured.

    You will need some coding assistance. If you are comfortable with code, you can check number of posts of a user when the publish it and mark them as featured.

    https://stackoverflow.com/questions/40744782/what-is-the-wordpress-hook-when-i-publish-a-new-post-not-when-i-update-a-publi

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45934

    I made a rough attempt

     add_action( 'publish_post', function () {
    
      $site_users = get_users( array( 'fields' => 'ID' ) );
      
      $users_post_count = ts_get_user_posts_count($site_users, array(
        'post_type' =>'post',
        'post_status'=> 'published'
      ));
    
      if ( 24 > $users_post_count ) {
    		return;
      }
      
      bp_featured_members()->add_user( $user_id );
    } );  
  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45956

    Kind reminder sir

    Thanks

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

    Hi Tosin,
    Please give it a try and let me know if it works or not?

    
    
    add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
    
    	if ( 'publish' !== $new_status || ! function_exists( 'bp_featured_members' ) ) {
    		return;
    	}
    
    	$user = get_user_by( 'id', $post->post_author );
    
    	if ( ! $user ) {
    		return;
    	}
    
    	$users_post_count = count_user_posts( $user->ID, 'post' );
    
    	if ( 25 == $users_post_count && ! bp_featured_members()->is_featured( $user->ID ) ) {
    		bp_featured_members()->add_user( $user->ID );
    	}
    
    }, 10, 3 );
    
    

    Regards
    Brajesh

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

    it strictly marks on 2. If you want to mark any user who has more than 24 posts(e.g 26, 28 etc), you change the condition from

    
    if ( 25 == $users_post_count && ! bp_featured_members()->is_featured( $user->ID ) ) {
    		bp_featured_members()->add_user( $user->ID );
    	}
    

    to

    
    if ( $users_post_count >= 25  && ! bp_featured_members()->is_featured( $user->ID ) ) {
    		bp_featured_members()->add_user( $user->ID );
    	}
    

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45966

    Thanks Brajesh

    Can you improve this a little further to prevent user abuse especially when users publish below 25 posts

    If post count = 25 mark user as featured
    but
    If user deletes a post and post count is below 25 mark user as unfeatured

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

    Hi Tosin,
    The problem is we are not storing any flag/marker to differentiate a user who has been marked as featured manually by the site admin.

    So, if we start automating the non featuring of user, it will impact that.

    Is that fine with you?

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45984

    Yes sir

    Thanks for your assistance

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

    Hi Tosin,
    Here is your updated code

    
    
    add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
    
    	if ( 'publish' !== $new_status || ! function_exists( 'bp_featured_members' ) ) {
    		return;
    	}
    
    	$user = get_user_by( 'id', $post->post_author );
    
    	if ( ! $user ) {
    		return;
    	}
    
    	$users_post_count = count_user_posts( $user->ID, 'post' );
    
    	$is_featured = bp_featured_members()->is_featured( $user->ID );
    
    	if( 25 == $users_post_count && ! $is_featured ){
    		bp_featured_members()->add_user( $user->ID );
    	} elseif( $is_featured && $users_post_count< 25 ) {
    		bp_featured_members()->remove_user( $user->ID );
    	}
    
    }, 10, 3 );
    
    

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved