BuddyDev

Search

[Resolved] Attribute post on account deletion in buddypress

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

    Hello,

    Please what could be wrong with this code I cant get it to work. I want all published posts to be assigned to a specific user when user accounts are being deleted in the frontend.

     // Reassign and attribute post of deleted users to the ghost member with ID 
    function reassign_posts_on_user_deletion_to_ghostwriter($user_id, $reassign) {
        // Check if the deleted user is not the target user (466526)
        if ($user_id !== 4767868) {
            $args = array(
                'post_type' => 'post',
                'author' => $user_id, // Posts authored by the deleted user
                'posts_per_page' => -1, // Get all posts
                'fields' => 'ids', // Retrieve only post IDs
                'post_status' => 'publish', // Only published posts
            );
            $posts = get_posts($args);
            // Reassign published posts to user with ID 466526
            foreach ($posts as $post_id) {
                $post_status = get_post_status($post_id);
                if ($post_status === 'publish') {
                    wp_update_post(array(
                        'ID' => $post_id,
                        'post_author' => 4767868,
                    ));
                }
            }
        }
    }
    add_action('deleted_user', 'reassign_posts_on_user_deletion_to_ghostwriter', 10, 2);
     
  • Participant
    Level: Guru
    Posts: 900
    Tosin on #50888

    Hi Brajesh

    I finally got it to work with this code, kindly review

    I really think this would help with buddyblog when multiple authors delete their account, lots of 404 errors would be prevented

    Also note that the ghostwriter account is just a non active account used to preserve useful content from deleted accounts.

     function reassign_posts_on_user_deletion_to_ghostwriter($user_id) {
        // Check if the deleted user is not the target user (466526)
        if ($user_id !== 466526) {
            $args = array(
                'post_type'      => 'post',
                'author'         => $user_id, // Posts authored by the deleted user
                'posts_per_page' => -1,       // Get all posts
                'fields'         => 'ids',    // Retrieve only post IDs
                'post_status'    => 'publish',// Only published posts
            );
            $posts = get_posts($args);
            // Reassign published posts to user with ID 466526
            foreach ($posts as $post_id) {
                $post_status = get_post_status($post_id);
                if ($post_status === 'publish') {
                    wp_update_post(array(
                        'ID'          => $post_id,
                        'post_author' => 466526,
                    ));
                }
            }
        }
    }
    add_action('bp_core_pre_delete_account', 'reassign_posts_on_user_deletion_to_ghostwriter', 10, 1);
     
  • Participant
    Level: Guru
    Posts: 900
    Tosin on #50889

    it might also be advisable to also use this code to disable comments on posts attributed to the non active user

     function disable_comments_on_single_post_by_user() {
        if (is_singular('post')) {
            $post_id = get_queried_object_id(); // Get the current post ID
    
            // Get the post's author ID and comment status
            $post_author_id = get_post_field('post_author', $post_id);
            $comment_status = get_post_field('comment_status', $post_id);
    
            // Check if the post author's ID matches the specified user ID and comments are open
            if ($post_author_id == 466526 && $comment_status === 'open') {
                // Update post's comment status to 'closed'
                wp_update_post(array(
                    'ID'             => $post_id,
                    'comment_status' => 'closed',
                ));
            }
        }
    }
    add_action('template_redirect', 'disable_comments_on_single_post_by_user');
     
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #50908

    Hello Tosin,

    Please try the following code

    
    function reassign_posts_on_user_deletion_to_ghostwriter( $user_id ) {
    	// Check if the deleted user is not the target user (466526)
    	if ( $user_id !== 466526 ) {
    		$args  = array(
    			'post_type'      => 'post',
    			'author'         => $user_id, // Posts authored by the deleted user
    			'posts_per_page' => - 1,       // Get all posts
    			'fields'         => 'ids',    // Retrieve only post IDs
    			'post_status'    => 'publish',// Only published posts
    		);
    		$posts = get_posts( $args );
    		// Reassign published posts to user with ID 466526
    		foreach ( $posts as $post_id ) {
    			// As we are fetching only published posts of the deleted user.
    			// $post_status = get_post_status( $post_id );
    			// if ( $post_status === 'publish' ) {
    			wp_update_post( array(
    				'ID'             => $post_id,
    				'post_author'    => 466526,
    				'comment_status' => 'closed',
    			) );
    			//}
    		}
    	}
    }
    
    add_action( 'bp_core_pre_delete_account', 'reassign_posts_on_user_deletion_to_ghostwriter', 10, 1 );
    
    

    You can make commenting disabled at the time of re-assigning the published post. Please give it a try.

    Regards
    Ravi

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

    You are awesome Ravi, thanks a lot

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

    Hello Tosin,

    Thank you so much for your kind words! I’m really glad I could help.

    Regards
    Ravi

The topic ‘ [Resolved] Attribute post on account deletion in buddypress’ is closed to new replies.

This topic is: resolved