0

What am I doing wrong on this query? The main issue is the var_dump returns null. The query works fine in phpMyAdmin.

global $wpdb;
$users = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT 
            u.ID AS id, 
            u.user_login AS name,
            AVG(b.star) AS rating, 
            COUNT(b.star) AS reviews,
            m.meta_value AS roles
        FROM
            wp_users AS u
                LEFT JOIN
            wp_usermeta AS m ON u.ID = m.user_id
                LEFT JOIN
            wp_bp_activity AS b ON u.ID = b.usercheck
        WHERE
            (m.meta_key = 'wp_capabilities' AND m.meta_value LIKE '%subscriber%') 
        AND 
            (b.is_activated is null or b.is_activated = 1)
        GROUP BY u.ID
        ORDER BY reviews DESC
        LIMIT %d", $limit
    )
);
return var_dump($users);
0

Be careful with the '%subscriber%' part, inside the wpdb::prepare() method.

Use '%%subscriber%%' instead.

Otherwise $limit will match the %s part and the %d part will be unmatched.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.