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


$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.

  • Oh I see. I thought $wpdb-> will add prefix with custom table as well. Thanks to clarify this was new to me. 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. 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";
  • 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. Feb 23 '13 at 17:07
  • 1
    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
  • Thank's for editing kaiser. Should have been that simple in the first place. :-)
    – s1lv3r
    Feb 23 '13 at 18:06

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.