根据某些条件检索用户列表

时间:2015-01-25 作者:LaurentiuZ

我在这个问题上做错了什么?主要问题是var_dump 返回null。该查询在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);

1 个回复
最合适的回答,由SO网友:birgire 整理而成

小心\'%subscriber%\' 零件,内部wpdb::prepare() 方法

使用\'%%subscriber%%\' 相反

否则$limit 将匹配%s 零件和%d 零件将不匹配。

结束

相关推荐

wpdb custom post_type problem

我想在自定义SQL查询中使用自定义帖子类型,但是,当我在我的查询中添加自定义帖子类型时,我没有得到任何结果。我的问题在哪里?这是我试过的代码$top5_ov_performance = $wpdb->get_results(\"SELECT post_id,meta_value FROM \".$wpdb->prefix.\"postmeta WHERE meta_key = \'sd_top5_overclocing\' AND (post_type = \'cpu\') ORDER BY