skip to Main Content

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


  1. Since you are already using array_filter, you can extend the filter and remove some code, excluding also the current user:

    if ( $total_post > 0 ) {
        $i = 0;  // is this necessary?? 
        foreach ( $authpr_post as $row ) {        //echo '<br/>ID...'.$row->ID;
            $user_list      = get_post_meta( $row->ID, '_favorites', TRUE );
            // REMOVE $user_list_arr2 = [];
            // SUBSTITUTE $user_list_arr  = array_filter( explode( ",", $user_list ), 'strlen' );
            $user_list_arr = array_filter(explode(',', $user_list), fn ($v) => ($tv = trim($v)) && $tv != $current_user->ID);
            
            /*  REMOVE
            $i              = 0;
            foreach ( $user_list_arr as $arr ) {
                if ( trim( $arr ) != '' ) {
                    $user_list_arr2[ $i ] = $arr;
                    $i ++;
                }
            }
            */
            
            // Now you must use $user_list_arr instead of $user_list_arr2
            
            if ( sizeof($user_list_arr) > 0 ) {  // you could use count($user_list_arr) as you did for $total_post
    
                $args_users = ['include' => $user_list_arr];
                // 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 );                                                 
    
    Login or Signup to reply.
    • I have never been a WordPress developer.
    • There are iterated trips to the database — these are not best practice; table JOINing should be implemented instead.
    • There are comma-separated values in database tables — this is not best practice; the database should be normalized.
    • My snippet below is not tested.

    1. Use a prepared statement to fetch all qualifying posts.
    2. Iterate those results and build the include array using preg_split().
    3. Build the regex pattern to destroy potential spaces next to delimiting commas and also treat the current user’s id as a delimiter so that it is not included in the returned array.
    4. Use PREG_SPLIT_NO_EMPTY() to filter out any empty values.

    Code:

    $current_user_posts = $wpdb->get_results(
        $wpdb->prepare(
            <<<SQL
                SELECT *
                FROM $wpdb->posts
                WHERE post_author=%d
                      AND post_type IN (%s,%s)
                      AND post_status IN ('publish','pending','draft')
            SQL,
            [$current_user->ID, $directory_url_1, $directory_url_2]
        )
    );
    
    foreach ($current_user_posts as $post) {
        $args_users = [
            'include' => preg_split(
                "/ *, *|b{$current_user->ID}b/",
                trim(get_post_meta($post->ID, '_favorites', TRUE)),
                0,
                PREG_SPLIT_NO_EMPTY
            )
        ];
        foreach ((new WP_User_Query($args_users))->get_results() as $user) {
            // do whatever you need to do
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search