WordPress: So I’ve been calling wp_(tablenames) directly in mysql query. What’s the best way to correct this?

So I’ve been developing a plugin in wordpress, which has been running very slowly, and have been making database calls directly to wp_usermeta, which I’ve just found out you should never call directly, as follows:

Read More
$getUserdaetails = pdo_query("Select wp_usermeta.user_id, Min(Case meta_key When 'name_of_your_facility' Then meta_value End) GYM_NAME, Min(Case meta_key When 'description_of_the_gym' Then meta_value End) GYM_DESCRIPTION, Min(Case meta_key When 'city_registration' Then meta_value End) GYM_CITY, Min(Case meta_key When 'country_registration' Then (SELECT DISTINCT COUNTRY_MAPPING.COUNTRY_NAME FROM wp_usermeta, COUNTRY_MAPPING WHERE wp_usermeta.meta_key = 'country_registration' AND wp_usermeta.meta_value = COUNTRY_MAPPING.COUNTRY_CODE AND wp_usermeta.user_id = ?) End) GYM_COUNTRY, Min(Case meta_key When 'category' Then (SELECT DISTINCT CATEGORY_MAPPING.CATEGORY_NAME FROM wp_usermeta, CATEGORY_MAPPING WHERE wp_usermeta.meta_key = 'category' AND wp_usermeta.meta_value = CATEGORY_MAPPING.CATEGORY_CODE AND wp_usermeta.user_id  = ?) End) GYM_CATEGORY, Min(Case meta_key When 'image_1' Then (SELECT wp_posts.guid FROM wp_usermeta, wp_posts WHERE wp_usermeta.user_id = ? AND wp_usermeta.meta_key = 'image_1' AND wp_posts.post_type = 'attachment' AND wp_usermeta.meta_value = wp_posts.ID) End) GYM_IMAGE FROM wp_usermeta, wp_posts, COUNTRY_MAPPING WHERE user_id = ?", pdo_real_escape_string($row1[user_id]), pdo_real_escape_string($row1[user_id]), pdo_real_escape_string($row1[user_id]), pdo_real_escape_string($row1[user_id]));
                     
$recordCountTest2 .= pdo_num_rows($getUserdaetails);
foreach($getUserdaetails as $row2){
 $html3a .= "Category - <span style='color: #000000;'>{$row2[GYM_CATEGORY]}</span>
 .........
 ";
}

My question is, how do I best change my code so that it becomes wordpress/plugin compliant?

Thanks

Related posts

Leave a Reply

1 comment