为什么我的wordpress sql查询不返回结果?

时间:2018-09-05 02:04:02

标签: php sql wordpress

我写了这个查询来从多个wordpress表中输出结果,但是由于某种原因,它只是返回空白。也许新鲜的眼睛可以发现我的查询问题。

global $wpdb;

$sql = "SELECT 
            u.ID,
            u.user_email,
            u.display_name,
            m.user_id,
            m.first_name,
            m.last_name,
            m.description,
            m.company_name,
            m.company_website,
            m.phone,
            m.company_address,
            m.scategory,
            m.facebook,
            m.twitter,
            m.linkedin,
            m.youtube,
            m.instagram,
            m.googleplus,
            m.specialties,
            m.company_logo,
            t.user_id,
            t.pack_id 
       FROM $wpdb->users u 
       INNER JOIN $wpdb->usermeta m ON m.user_id = u.ID 
       INNER JOIN $wpdb->wpuf_transaction t ON t.user_id = u.ID 
       WHERE m.meta_key = 'wp_capabilities' 
       AND m.meta_value LIKE '%employer%' 
       ORDER BY u.user_registered";

$results = $wpdb->get_results($sql);

foreach ($results as $users) {
    echo $users.'<br/>';
}

2 个答案:

答案 0 :(得分:0)

尝试这种格式。

$wpdb->get_results('SQL HERE', OBJECT);

答案 1 :(得分:0)

这个问题是我没有正确引用我的meta_keys。

$sql = "SELECT DISTINCT 
            u.ID,
            u.user_email,
            u.display_name,
            m.user_id,
            m1.meta_value AS first_name,
            m2.meta_value AS last_name,
            m3.meta_value AS descriptions,
            m4.meta_value AS company_name,
            m5.meta_value AS company_website,
            m6.meta_value AS phone,
            m7.meta_value AS company_address,
            m8.meta_value AS scategory,
            m9.meta_value AS facebook,
            m10.meta_value AS twitter,
            m11.meta_value AS linkedin,
            m12.meta_value AS youtube,
            m13.meta_value AS instagram,
            m14.meta_value AS googleplus,
            m15.meta_value AS specialties,
            m16.meta_value AS company_logo,
            t.user_id,
            t.pack_id
       FROM $wpdb->users u 
       INNER JOIN $wpdb->usermeta m ON (m.user_id = u.ID)
       INNER JOIN $wpdb->usermeta m1 ON (m1.user_id = u.ID AND m1.meta_key = 'first_name')
       INNER JOIN $wpdb->usermeta m2 ON (m2.user_id = u.ID AND m2.meta_key = 'last_name')
       INNER JOIN $wpdb->usermeta m3 ON (m3.user_id = u.ID AND m3.meta_key = 'description')
       INNER JOIN $wpdb->usermeta m4 ON (m4.user_id = u.ID AND m4.meta_key = 'company_name')
       INNER JOIN $wpdb->usermeta m5 ON (m5.user_id = u.ID AND m5.meta_key = 'company_website')
       INNER JOIN $wpdb->usermeta m6 ON (m6.user_id = u.ID AND m6.meta_key = 'phone')
       INNER JOIN $wpdb->usermeta m7 ON (m7.user_id = u.ID AND m7.meta_key = 'company_address')
       INNER JOIN $wpdb->usermeta m8 ON (m8.user_id = u.ID AND m8.meta_key = 'scategory')
       INNER JOIN $wpdb->usermeta m9 ON (m9.user_id = u.ID AND m9.meta_key = 'facebook')
       INNER JOIN $wpdb->usermeta m10 ON (m10.user_id = u.ID AND m10.meta_key = 'twitter')
       INNER JOIN $wpdb->usermeta m11 ON (m11.user_id = u.ID AND m11.meta_key = 'linkedin')
       INNER JOIN $wpdb->usermeta m12 ON (m12.user_id = u.ID AND m12.meta_key = 'youtube')
       INNER JOIN $wpdb->usermeta m13 ON (m13.user_id = u.ID AND m13.meta_key = 'instagram')
       INNER JOIN $wpdb->usermeta m14 ON (m14.user_id = u.ID AND m14.meta_key = 'googleplus')
       INNER JOIN $wpdb->usermeta m15 ON (m15.user_id = u.ID AND m15.meta_key = 'specialties')
       INNER JOIN $wpdb->usermeta m16 ON (m16.user_id = u.ID AND m16.meta_key = 'company_logo')
       INNER JOIN $wpdb->wpuf_transaction t ON t.user_id = u.ID
       WHERE m.meta_key = 'wp_capabilities' 
       AND m.meta_value LIKE '%employer%' 
       ORDER BY u.ID";