Я застрял с этим, как я могу обновить эту функцию, если один из наших пользователей удалил свою учетную запись?
Это должно обновить это самостоятельно, и я не знаю, что я могу сделать, чтобы обновить автоматически.
Любая помощь, пожалуйста.
/**
* Retrieve following count
*
* Gets the total number of users that the specified user is following
*
* @access private
* @since 1.0
* @param int $user_id - the ID of the user to retrieve a count for
* @return int
*/
function pwuf_get_following_count( $user_id = 0 ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$following = pwuf_get_following( $user_id );
$count = 0;
if ( $following ) {
$count = count( $following );
}
return (int) apply_filters( 'pwuf_get_following_count', $count, $user_id );
}
вот функция для получения следующих пользователей
/**
* Retrieves all users that the specified user follows
*
* Gets all users that $user_id followers
*
* @access private
* @since 1.0
* @param int $user_id - the ID of the user to retrieve following for
* @return array
*/
function pwuf_get_following( $user_id = 0 ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$following = get_user_meta( $user_id, '_pwuf_following', true );
if ( empty( $following ) ) {
return;
}
return (array) apply_filters( 'pwuf_get_following', $following, $user_id );
}
И этот для увеличения или уменьшения числа пользователей.
/**
* Increase follower count
*
* Increments the total count for how many users a specified user is followed by
*
* @access private
* @since 1.0
* @param int $user_id - the ID of the user to increease the count for
* @return int
*/
function pwuf_increase_followed_by_count( $user_id = 0 ) {
do_action( 'pwuf_pre_increase_followed_count', $user_id );
$followed_count = pwuf_get_follower_count( $user_id );
if ( $followed_count !== false ) {
$new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', $followed_count + 1 );
} else {
$new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', 1 );
}
do_action( 'pwuf_post_increase_followed_count', $user_id );
return $new_followed_count;
}
/**
* Decrease follower count
*
* Decrements the total count for how many users a specified user is followed by
*
* @access private
* @since 1.0
* @param int $user_id - the ID of the user to decrease the count for
* @return int
*/
function pwuf_decrease_followed_by_count( $user_id ) {
do_action( 'pwuf_pre_decrease_followed_count', $user_id );
$followed_count = pwuf_get_follower_count( $user_id );
if ( $followed_count ) {
$count = update_user_meta( $user_id, '_pwuf_followed_by_count', ( $followed_count - 1 ) );
do_action( 'pwuf_post_increase_followed_count', $user_id );
}
return $count;
}
Теперь я не знаю, почему счет не изменился, когда некоторые пользователи удалили свои учетные записи.
Есть резервное копирование вашей базы данных перед тем, как попробовать это, или попробуйте использовать на промежуточном сайте, прежде чем внедрять в живую. Добавьте это в файл functions.php вашей темы.
function follow_delete_user( $user_id ) {
$userslist = get_users();
foreach ( $userslist as $user ) {
pwuf_unfollow_user( $user->ID, $user_id );
pwuf_unfollow_user( $user_id, $user->ID );
}
}
add_action( 'delete_user', 'follow_delete_user' );
Других решений пока нет …