WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know it should work with custom table and I think it worked in previous development I believe. But this time I don't know whey it is not getting custom table with $wpdb->tablename


global $wpdb;    
$items = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->review_media"));

This is not giving any result but when I tried with `$wpdb->prefix than it works

$wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."review_media"));

Any idea

share|improve this question
up vote 4 down vote accepted

$wpdb->tablename works only with the tables initially present in a WordPress installation. These tables are defined in wp-includes/wp-db.php.

The second method you used is perfectly acceptable.

share|improve this answer
Oh I see. I thought $wpdb-> will add prefix with custom table as well. Thanks to clarify this was new to me. – pixelngrain Feb 23 '13 at 17:09
@pixelngrain, you can add your table name like that. It does work. I tried it, but I don't know if it will get you trouble later on. – s_ha_dum Feb 23 '13 at 17:15
Yeh that is why I want to use prfix dynamically as I am developing plugin and prefix can be changed, depending on the installation. Thanks a lot again and using my second method. – pixelngrain Feb 23 '13 at 17:27

You are supposed to store the table name in $wpdb before you can use it the way you did.

$wpdb->review_media = "{$wpdb->prefix}review_media";
share|improve this answer
Do I need to replace $tableprefix with my prefix? or that is the class variable? Because I have added this before use but still happens nothing. – pixelngrain Feb 23 '13 at 17:07
Yes, you would need to fill this variable yourself. But you can get the prefix out of $wpdb->prefix. – s1lv3r Feb 23 '13 at 17:16
Oh I see. Thanks a lot for your help – pixelngrain Feb 23 '13 at 17:26
Oh yeah this is great solution now.. thanks – pixelngrain Feb 23 '13 at 17:38
Thank's for editing kaiser. Should have been that simple in the first place. :-) – s1lv3r Feb 23 '13 at 18:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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