I’m trying to exclude the current user from a saved list. My code is including every user that has saved a user but I don’t want the current user in the list. How does one exclude the current user? Here is my code.
<?php
$sql = "SELECT * FROM $wpdb->posts WHERE post_type IN ('" . $directory_url_1 . "','" . $directory_url_2 . "' ) and post_author='" . $current_user->ID . "' and post_status IN ('publish','pending','draft' ) ";
$authpr_post = $wpdb->get_results( $sql );
$total_post = count( $authpr_post );
$iv_redirect_user = get_option( '_iv_directories_profile_public_page' );
$reg_page_user = '';
if ( $iv_redirect_user != 'defult' ) {
$reg_page_user = get_permalink( $iv_redirect_user );
}
if ( $total_post > 0 ) {
$i = 0;
foreach ( $authpr_post as $row ) { //echo '<br/>ID...'.$row->ID;
$user_list = get_post_meta( $row->ID, '_favorites', TRUE );
$user_list_arr2 = [];
$user_list_arr = array_filter( explode( ",", $user_list ), 'strlen' );
$i = 0;
foreach ( $user_list_arr as $arr ) {
if ( trim( $arr ) != '' ) {
$user_list_arr2[ $i ] = $arr;
$i ++;
}
}
if ( sizeof( $user_list_arr2 ) > 0 ) {
$args_users = ['include' => $user_list_arr2,];
// The User Query
$user_query = new WP_User_Query( $args_users );
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
//print_r( $user );
?>
I tried adding this string but it did not work. Oh, I’m also very new at this and may have not added it properly.
$filtered_user = user.objects.exclude(id=request.user.id)
$print("ALL USERS:" + str(filtered_user))
args = {
'user': user,
$'filtered_user':
2
Answers
Since you are already using array_filter, you can extend the filter and remove some code, excluding also the current user:
include
array usingpreg_split()
.PREG_SPLIT_NO_EMPTY()
to filter out any empty values.Code: