• Resolved sixtyseven

    (@sixtyseven)


    I get an error message when I try to filter the users list by status:

    Unknown column 'wp_usermeta.meta_key' in 'where clause' für Abfrage SELECT SQL_CALC_FOUND_ROWS prefix_users.ID FROM svda2016_users INNER JOIN prefix_usermeta ON ( prefix_users.ID = wp_usermeta.user_id ) WHERE 1=1 AND ( (wp_usermeta.meta_key = 'pw_user_status' AND CAST(wp_usermeta.meta_value AS CHAR) = 'pending') ) ORDER BY user_login ASC LIMIT 0, 20 von WP_Users_List_Table->prepare_items, WP_User_Query->__construct, WP_User_Query->query

    I checked where this query was triggered and found an Error in the function ‘filter_by_status()’ in tzhe file includes/user-list.php: There are hardcoded table prefixes wp_ in it, where it should have dynamic table names. So here ist the fixed function, works like a charm now.

    /**
    	 * Modify the user query if the status filter is being used.
    	 *
    	 * @uses pre_user_query
    	 * @param $query
    	 */
    	public function filter_by_status( $query ) {
    		global $wpdb;
    
    		if ( !is_admin() ) {
    			return;
    		}
    
    		$screen = get_current_screen();
    		if ( isset( $screen ) && 'users' != $screen->id ) {
    			return;
    		}
    
    		if ( $this->selected_status() != null ) {
    			$filter = $this->selected_status();
    
    			$query->query_from .= " INNER JOIN {$wpdb->usermeta} ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id )";
    
    			if ( 'approved' == $filter ) {
    				$query->query_fields = "DISTINCT SQL_CALC_FOUND_ROWS {$wpdb->users}.ID";
    				$query->query_from .= " LEFT JOIN {$wpdb->usermeta} AS mt1 ON ({$wpdb->users}.ID = mt1.user_id AND mt1.meta_key = 'pw_user_status')";
    				$query->query_where .= " AND ( ( {$wpdb->usermeta}.meta_key = 'pw_user_status' AND CAST({$wpdb->usermeta}.meta_value AS CHAR) = 'approved' ) OR mt1.user_id IS NULL )";
    			} else {
    				$query->query_where .= " AND ( ({$wpdb->usermeta}.meta_key = 'pw_user_status' AND CAST({$wpdb->usermeta}.meta_value AS CHAR) = '{$filter}') )";
    			}
    		}
    	}

    Others than that: I find this plugin absolutely useful. Would be a real pitty, if the author has abandoned it …

    • This topic was modified 9 years, 1 month ago by sixtyseven.

The topic ‘SQL Error when trying to show pending users’ is closed to new replies.