Support » Developing with WordPress » Exclude current user in SQL

  • Hi,

    I have a plugin in the works that I’m trying to modify, and I’m having no luck with getting the current user ID excluded from a sql statement that displays a list of users that have liked a post.

    /**
     * Return user names that liked a post
     * !!try to exclude the current user!!
     * @global $wpdb
     * @param int $pid Post ID
     * @return array Users that liked the post
     */
    function like_get_named( $pid ) {
    	global $wpdb;
    
    	$results = $wpdb->get_results ( $wpdb->prepare( "SELECT like_uid FROM " . $wpdb->prefix . "likes WHERE like_pid=%d", $pid), ARRAY_N );
    
            if($results === null)
    	return apply_filters('like_get_named', array());
            $results = array_map('_like_flatten_array', $results);
            return apply_filters('like_get_named', $results);
    }

    So I’m looking for a facebookesque display like

    You, author1 and author2 liked this post

    But instead I’m getting

    You, my-name and author1 and author2 liked this post

Viewing 1 replies (of 1 total)
  • Try adding a NOT IN to your query. Something like:

    global $user_ID;
    
    $results = $wpdb->get_results ( $wpdb->prepare( "SELECT like_uid FROM " . $wpdb->prefix . "likes WHERE like_pid=%d AND like_uid NOT IN(%d)", $pid, $user_ID), ARRAY_N );
Viewing 1 replies (of 1 total)
  • The topic ‘Exclude current user in SQL’ is closed to new replies.